praxis

Various programming exercises.
Log | Files | Refs

commit cfa32faf70488b57623324ec3b07c2292ee5c295
parent 684f90ec80cd696e2ec0277b3136d85a96b282ef
Author: Jared Tobin <jared@jtobin.ca>
Date:   Mon,  2 Mar 2015 22:22:52 +1300

Add color exercise.

Diffstat:
A20141111_color/FavoriteColor.hs | 25+++++++++++++++++++++++++
A20141111_color/test.dat | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 135 insertions(+), 0 deletions(-)

diff --git a/20141111_color/FavoriteColor.hs b/20141111_color/FavoriteColor.hs @@ -0,0 +1,25 @@ +{-# OPTIONS_GHC -Wall #-} + +module Main where + +import Control.Arrow +import Data.Function +import Data.List +import System.Environment + +parse :: String -> String +parse input = fst $ maximumBy (compare `on` snd) colors where + recs = filter ((== "favoritecolor") . fst) . fmap record $ lines input + colors = fmap ((head . nub) &&& length) . group . sort $ fmap snd recs + +record :: String -> (String, String) +record rec = (key, val) where + alphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" + key = takeWhile (/= ':') rec + val = dropWhile (`notElem` alphaNum) . dropWhile (/= ':') $ rec + +main :: IO () +main = getArgs >>= \args -> case args of + [] -> putStrLn "USAGE: ./favorite <FILE>" + (file:_) -> readFile file >>= putStrLn . parse + diff --git a/20141111_color/test.dat b/20141111_color/test.dat @@ -0,0 +1,110 @@ +header: thingy +hater: thungy +foo: bar +favoritecolor: blue + +header: thingy +hater: thungy +foo: bar +favoritecolor: green + +header: thingy +hater: thungy +foo: bar +favoritecolor: blue + +header: thingy +hater: thungy +foo: bar +favoritecolor: green + +header: thingy +hater: thungy +foo: bar +favoritecolor: yellow + +header: thingy +hater: thungy +foo: bar +favoritecolor: orange + +header: thingy +hater: thungy +foo: bar +favoritecolor: orange + +header: thingy +hater: thungy +foo: bar +favoritecolor: green + +header: thingy +hater: thungy +foo: bar +favoritecolor: blue + +header: thingy +hater: thungy +foo: bar +favoritecolor: red + +header: thingy +hater: thungy +foo: bar +favoritecolor: blue + +header: thingy +hater: thungy +foo: bar +favoritecolor: green + +header: thingy +hater: thungy +foo: bar +favoritecolor: blue + +header: thingy +hater: thungy +foo: bar +favoritecolor: yellow + +header: thingy +hater: thungy +foo: bar +favoritecolor: orange + +header: thingy +hater: thungy +foo: bar +favoritecolor: orange + +header: thingy +hater: thungy +foo: bar +favoritecolor: orange + +header: thingy +hater: thungy +foo: bar +favoritecolor: blue + +header: thingy +hater: thungy +foo: bar +favoritecolor: red + +header: thingy +hater: thungy +foo: bar +favoritecolor: red + +header: thingy +hater: thungy +foo: bar +favoritecolor: green + +header: thingy +hater: thungy +foo: bar +favoritecolor: green +