[6 kyu] Who likes it?

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 functionlikes :: [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"

Solution :

def likes(name):
    result = ""
    if len(name) == 0:
        result = "no one likes this"
    elif len(name) == 1:
         result = name[0] + " likes this"
    elif len(name) == 2:
        result = name[0] + " and " + name[1] + " like this"
    elif len(name) == 3:
        result = name[0] + ", " + name[1] + " and " + name[2] + " like this"
    else:
        rest_member = len(name) - 2
        result = name[0] + ", " + name[1] + " and " + str(rest_member) + " others like this"
    return result
    #your code here
发布了16 篇原创文章 · 获赞 0 · 访问量 50

猜你喜欢

转载自blog.csdn.net/HM_773_220/article/details/104764207