-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwho_likes_it.py
More file actions
38 lines (36 loc) · 1.61 KB
/
Copy pathwho_likes_it.py
File metadata and controls
38 lines (36 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def likes(names):
"""You probably know the "like" system from Facebook and other pages.
People can "like" blog posts, pictures or other items. We want to
create the text that should be displayed next to such an item.
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"
For more than 4 names, the number in and 2 others simply increases.
"""
if len(names) == 0:
return 'no one likes this'
elif len(names) == 1:
return (names[0] + ' likes this')
elif len(names) == 2:
return (names[0] + ' and ' + names[1] + ' like this')
elif len(names) == 3:
return (names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this')
else:
return (names[0] + ', ' + names[1] + ' and ' + str(len(names) - 2) + ' others like this')
# My solution is pretty sucky(alright its cool)
# solution found on solution page:
# def likes(names):
# n = len(names)
# return {
# 0: 'no one likes this',
# 1: '{} likes this',
# 2: '{} and {} like this',
# 3: '{}, {} and {} like this',
# 4: '{}, {} and {others} others like this'
# }[min(4, n)].format(*names[:3], others=n-2)