Skip to content

Instantly share code, notes, and snippets.

@CarlosMChica
Last active May 5, 2017 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosMChica/58aa2008e05b000716b791cfcb3b5119 to your computer and use it in GitHub Desktop.
Save CarlosMChica/58aa2008e05b000716b791cfcb3b5119 to your computer and use it in GitHub Desktop.
mostPopularLetterWithCount :: String -> (Char, Int)
mostPopularLetterWithCount xs =
swap . maximum . fmap swap . Map.toList . Map.fromListWith (+) $ [(x, 1) | x <- xs, not . isSpace $ x]
mostPopularWordWithCount :: String -> (String, Int)
mostPopularWordWithCount xs =
swap . maximum . fmap swap . Map.toList . Map.fromListWith (+) $ [(x, 1) | x <- words xs]
4. What was the most popular letter for each message?
mostPopularLetter :: String -> Char
mostPopularLetter = fst. mostPopularLetterWithCount
mostPopularWord :: String -> String
mostPopularWord = fst . mostPopularWordWithCount
5. What was the most popular letter overall? What was the most
popular word?
coolestLtr :: [String] -> Char
coolestLtr = mostPopularLetter . concat
coolestWord :: [String] -> String
coolestWord = mostPopularWord . unwords
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment