Last active
April 29, 2018 00:01
-
-
Save pete-murphy/21e6f9ac7afa6dd740bcbe7247ac381e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PhoneExercise where | |
import Data.Char | |
import Data.List (elemIndex) | |
import Data.Maybe (fromJust) | |
data DaPhone = DaPhone [(Digit, String)] deriving (Eq, Show) | |
type Digit = Char | |
type Presses = Int | |
phone = DaPhone [ ('1', " ") | |
, ('2', "ABC") | |
, ('3', "DEF") | |
, ('4', "GHI") | |
, ('5', "JKL") | |
, ('6', "MNO") | |
, ('7', "PQRS") | |
, ('8', "TUV") | |
, ('9', "WXYZ") | |
, ('0', "+_") | |
, ('*', "^") | |
, ('#', ".,") ] | |
convo :: [String] | |
convo = [ "Wanna play 20 questions", | |
"Ya", | |
"U 1st haha", | |
"Lol ok. Have u ever tasted alcohol", | |
"Lol ya", | |
"Wow ur cool haha. Ur turn", | |
"Ok. Do u think I am pretty lol", | |
"Lol ya", | |
"Just making sure rofl ur turn" ] | |
reverseTaps :: DaPhone -> Char -> [(Digit, Presses)] | |
reverseTaps (DaPhone (p:ps)) c | |
| elem c $ snd p | |
= (fst p, fromJust $ elemIndex c $ snd p) : [] | |
| elem (toUpper c) $ snd p | |
= ('*', 1) | |
: (fst p, fromJust $ elemIndex (toUpper c) $ snd p) : [] | |
| c == fst p | |
= (fst p, 4) : [] | |
| otherwise | |
= reverseTaps (DaPhone ps) c | |
cellPhonesDead :: DaPhone -> String -> [(Digit, Presses)] | |
cellPhonesDead p = concatMap $ reverseTaps p | |
fingerTaps :: [(Digit, Presses)] -> Presses | |
fingerTaps = foldr ((+) . snd) 0 | |
type Freq = Int | |
incFreq' :: Eq a => a -> (a, Freq) -> (a, Freq) | |
incFreq' c x@(c', f) | |
| c == c' = (c, f + 1) | |
| otherwise = x | |
incFreq :: Eq a => a -> [(a, Freq)] -> [(a, Freq)] | |
incFreq c xs | |
| elem c $ map fst xs | |
= map (incFreq' c) xs | |
| otherwise | |
= (c, 1) : xs | |
freqs :: Eq a => [a] -> [(a, Freq)] | |
freqs = foldr incFreq [] | |
maxFreqs :: (Eq a, Ord a) => [(a, Freq)] -> (a, Freq) | |
maxFreqs (x:[]) = x | |
maxFreqs (x:y:xs) | |
| (snd x) > (snd y) | |
= maxFreqs (x:xs) | |
| otherwise | |
= maxFreqs (y:xs) | |
mostPopularLetter :: String -> Char | |
mostPopularLetter = fst . maxFreqs . freqs | |
mostPopularLetterCost :: String -> Int | |
mostPopularLetterCost = | |
(\(c, f) -> | |
(fingerTaps $ reverseTaps phone c) * f) . | |
maxFreqs . freqs | |
coolestLtr :: [String] -> Char | |
coolestLtr = mostPopularLetter . concat | |
coolestWord :: [String] -> String | |
coolestWord = fst . maxFreqs . freqs . words . concat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment