DEV Community

Discussion on: Daily Challenge #48 - Facebook Likes

Collapse
 
b2aff6009 profile image
b2aff6009

Python solution using a dict as switch case. Sadly I didn't had a cool idea about the "default" case wiithout using a default dict (didn't wanted to import anything)

def createString(persons): 
    defFunc = lambda x : "{}, {} and {} others like this".format(x[0], x[1], len(x)-2) 
    converter = {0 : lambda x : "{} likes this".format("no one"), 
                 1 : lambda x : "{} likes this".format(x[0]), 
                 2 : lambda x : "{} and {} like this".format(x[0], x[1]), 
                 3 : lambda x : "{}, {} and {} like this".format(x[0], x[1], x[2])} 
    return converter.get(len(persons), defFunc)(persons) 

assert(createString([]) == "no one likes this") 
assert(createString(["Peter"]) == "Peter likes this") 
assert(createString(["Jacob", "Alex"]) == "Jacob and Alex like this") 
assert(createString(["Max", "John", "Mark"]) == "Max, John and Mark like this") 
assert(createString(["Alex", "Jacob", "Mark", "Max"]) == "Alex, Jacob and 2 others like this")