Here's what I need to do: Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:
likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
Here's my code so far, only the 1st and second examples are correct, and I can't figure out how to convert ['Peter'] to 'Peter' and how to only put 'and' in the end(between last two people/values:
def likes(names):
if names == []:
return str("no one likes this")
elif len(names) == 1:
return (str(names)) + " likes this"
elif 1<len(names)<=3:
return " and ".join(map(str,names)) + " like this"
else:
return ", ".join(map(str,names)) + " and " + str(len(names)-2)+" others like this"
The output for the first case is: "['Peter'] likes this" (should equal 'Peter likes this') and for the last 2 cases: 'Max and John and Mark like this' (should equal 'Max, John and Mark like this'), 'Alex and Jacob and Mark and Max and 2 others like this' (should equal 'Alex, Jacob and 2 others like this')
I think I am quite close and would greatly appreciate any clarification!