Thanks to the effects of El Nino this year my holiday snorkelling trip was akin to being in a washing machine... Not fun at all.

Given a string made up of '~' and '\_' representing waves and calm respectively, your job is to check whether a person would become seasick.

Remember, only the process of change from wave to calm (and vice versa) will add to the effect (really wave peak to trough but this will do). Find out how many changes in level the string has and if that figure is more than 20% of the string, return "Throw Up", if less, return "No Problem".

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures

Starter code: def sea_sick(sea):
	

Solutions:
def sea_sick(sea):
	return 'Throw Up' if (sea.count('~_') + sea.count('_~')) / len(sea) > 0.2 else 'No Problem'


def sea_sick(sea):
	changes = sum((1 for (a, b) in zip(sea, sea[1:]) if a != b))
	return 'No Problem' if changes * 5 <= len(sea) else 'Throw Up'


def sea_sick(sea):
	waves = sum((sea[i:i + 2] in '_~_' for i in range(len(sea) - 1)))
	if waves / float(len(sea)) > 0.2:
		return 'Throw Up'
	return 'No Problem'


from operator import truediv

def sea_sick(s):
	result = sum((s[a:a + 2] in '_~_' for a in range(len(s) - 1)))
	if truediv(result, len(s)) > 0.2:
		return 'Throw Up'
	return 'No Problem'


sea_sick = lambda s: ['No Problem', 'Throw Up'][s.count('~_') + s.count('_~') > 0.2 * len(s)]


def sea_sick(sea):
	return 'Throw Up' if sum((x != y for (x, y) in zip(sea, sea[1:]))) / len(sea) > 0.2 else 'No Problem'


sea_sick = lambda s: ['No Problem', 'Throw Up'][sum(__import__('itertools').starmap(str.__ne__, zip(s, s[1:]))) / len(s) > 0.2]


def sea_sick(sea):
	(c, p) = (0, sea[0])
	for s in sea[1:]:
		if s != p:
			c += 1
		p = s
	return 'Throw Up' if c / len(sea) > 0.2 else 'No Problem'