⚠️ The world is in quarantine! There is a new pandemia that struggles mankind. Each continent is isolated from each other but infected people have spread before the warning. ⚠️

🗺️ You would be given a map of the world in a type of string:

    string s = "01000000X000X011X0X"

    '0' : uninfected

    '1' : infected

    'X' : ocean

⚫ The virus can't spread in the other side of the ocean.

⚫ If one person is infected every person in this continent gets infected too.

⚫ Your task is to find the percentage of human population that got infected in the end.

☑️ Return the percentage % of the total population that got infected.

❗❗ The first and the last continent are not connected!

💡 Example:

     start: map1 = "01000000X000X011X0X"
     end:   map1 = "11111111X000X111X0X"
     total = 15
     infected = 11
     percentage = 100*11/15 = 73.33333333333333

➕ For maps without oceans "X" the whole world is connected.

➕ For maps without "0" and "1" return 0 as there is no population.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Ad-hoc

Starter code: def infected(s):
	

Solutions:
def infected(s):
	lands = s.split('X')
	total = sum(map(len, lands))
	infected = sum((len(x) for x in lands if '1' in x))
	return infected * 100 / (total or 1)


def infected(s):
	total_population = s.split('X')
	total = 0
	infected = 0
	for population in total_population:
		if '1' in population:
			infected += len(population)
		total += len(population)
	try:
		return 100 * infected / total
	except ZeroDivisionError:
		return 0


def infected(s):
	total = infected = 0
	curr = isInfected = 0
	for c in s:
		if c == '0' or c == '1':
			if c == '1':
				isInfected = -1
			curr += 1
		elif c == 'X':
			infected += curr & isInfected
			total += curr
			isInfected = curr = 0
	infected += curr & isInfected
	total += curr
	return infected / (total / 100) if total else 0


def infected(s):
	inf = 0
	saf = 0
	for c in s.split('X'):
		if '1' in c:
			inf += len(c)
		else:
			saf += len(c)
	return 0 if inf + saf == 0 else inf / (inf + saf) * 100


def infected(s):
	total = len(s) - s.count('X')
	infected = sum([len(i) for i in s.split('X') if '1' in i])
	return infected / total * 100 if total > 0 else infected


import re

def infected(s):
	n = sum(map(len, re.findall('0*1[01]*', s)))
	s = len(s.replace('X', ''))
	return s and 100 * n / s


def infected(s):
	population = len(s) - s.count('X')
	return population and 100 * sum((len(continent) for continent in s.split('X') if '1' in continent)) / population


def infected(s):
	infected = 0
	total = 0
	for land in s.split('X'):
		if '1' in land:
			infected += len(land)
		total += len(land)
	return infected / total * 100 if total > 0 else 0


def infected(s):
	totalinf = 0
	total = 0
	p = s.split('X')
	for item in p:
		if '1' in item:
			totalinf += len(item)
			total += len(item)
		else:
			total += len(item)
	return float(100) * totalinf / total if total != 0 else 0


def infected(s):
	if '1' not in s:
		return 0
	s = s.split('X')
	return sum((len(i) for i in s if '1' in i)) / sum(map(len, s)) * 100