-1

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!

AVANISH RAJBHAR
  • 527
  • 3
  • 9
John Doe
  • 291
  • 1
  • 10
  • note: your list elements are already strings, so `map(str,names)` has no effect. – Adam.Er8 Feb 18 '20 at 14:23
  • You are indeed quite close, I'd suggest you keep testing it and solve it on your own, it's a good learning opportunity, and I don't think there's any preliminary knowledge you're missing and we can help with – Adam.Er8 Feb 18 '20 at 14:25

2 Answers2

1

You might want to look at the str.join() method:

>>> ",".join(["stack", "overflow"])                                                                                                                                                                            
>>> 'stack,overflow'

And this special case of slicing:

>>> l = [1,2,3]
>>> l[:-1]
[1, 2]
iCart
  • 2,179
  • 3
  • 27
  • 36
1

You can access the first item of the List with names[0]. I've also removed the unneeded map call.

def likes(names):
    if len(names) == 0:
        return "no one likes this"
    elif len(names) == 1:
        return names[0] + " likes this"
    elif 1 < len(names) <= 3:
        return " and ".join(names) + " like this"
    else:
        return ", ".join(names[0:2]) + f" and {str(len(names)-2)} others like this"

print(likes([]))                  # must be "no one likes this"
print(likes(["Peter"]))            # must be "Peter likes this"
print(likes(["Jacob", "Alex"]))    # must be "Jacob and Alex like this"
print(likes(["Max", "John", "Mark"]))          # must be "Max, John and Mark like this"
print(likes(["Alex", "Jacob", "Mark", "Max"])) # must be "Alex, Jacob and 2 others like this"
PHoughton
  • 61
  • 4
  • Thank you! Had to add another line for the 4th case to work(elif len(names) == 3: return ', '.join(names[0:2]) + f" and {str(names[-1])} like this") but couldn't have done it without you! – John Doe Feb 18 '20 at 15:54