Daily CodeFoo #2
Code Wars - Who likes it? - 6 Kyu
Problem
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:
Formalization
Input = [String]
Len Input >= 0
Outputs =
| "no one likes this"
| "{name1} likes this"
| "{name1} and {name2} like this"
| "{name1}, {name2} and {name3} like this"
| "{name1}, {name2} and {n-2} others like this"
Implementation
So the formalization is actually very similar to Haskell code and we can essentially do a one-to-one copy albeit with some string formatting.
It would be nice to take the string manipulation out into its own function. However, because CodeWars doesn’t like imports, all I could do is move the same code out into another function which seems pointless. Ideally we could use printf
from Text.Format
to generalize it a little more.
Summary
It’s nice to see how semi-formalizing a problem and its implementation can be so similar. This is a pretty simple problem but I still think it shows some of the perks of pattern matching in a language.