train kata "Who likes it?"

This commit is contained in:
Michael Schären 2026-03-13 19:06:10 +01:00
parent 4f1450fe6e
commit b11ca22119

View File

@ -0,0 +1,26 @@
def likes(names):
length = len(names[:4])
match length:
case 0:
return "no one likes this"
case 1:
return f"{names[0]} likes this"
case 2:
return f"{names[0]} and {names[1]} like this"
case 3:
return f"{names[0]}, {names[1]} and {names[2]} like this"
case _:
return f"{names[0]}, {names[1]} and {len(names) - 2} others like this"
if __name__ == "__main__":
assert likes([]) == "no one likes this"
assert likes(["Peter"]) == "Peter likes this"
assert likes(["Jacob", "Alex"]) == "Jacob and Alex like this"
assert likes(["Max", "John", "Mark"]) == "Max, John and Mark like this"
assert (
likes(["Alex", "Jacob", "Mark", "Max"]) == "Alex, Jacob and 2 others like this"
)
print("All tests passed!")