Last active
August 29, 2015 14:03
-
-
Save dabd/87e462ef6057cb5019cf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Golf where | |
| import Data.List | |
| import Data.Text (justifyRight, pack, unpack) | |
| import Data.Ord (comparing) | |
| -- ex 1 | |
| skips :: [a] -> [[a]] | |
| skips xs = map (flip every xs) [1..length xs] | |
| every :: Int -> [a] -> [a] | |
| every n xs = case drop (n-1) xs of | |
| (y:ys) -> y : every n ys | |
| [] -> [] | |
| -- ex 2 | |
| localMaxima :: [Integer] -> [Integer] | |
| localMaxima [] = [] | |
| localMaxima [x] = [] | |
| localMaxima [x,y] = [] | |
| localMaxima (x:y:z:zs) | |
| | y > x && y > z = y : localMaxima (y:z:zs) | |
| | otherwise = localMaxima (y:z:zs) | |
| -- ex 3 | |
| histLine :: (Integer, Int) -> String | |
| histLine (n, count) = replicate count '*' ++ "=" ++ show n | |
| frequencies :: [Integer] -> [(Integer, Int)] | |
| frequencies xs = map (\x -> (head x, length x)) . group . sort $ xs | |
| pad :: [String] -> [String] | |
| pad ss = map (\s -> unpack $ justifyRight maxLen ' ' $ pack s) ss | |
| where maxLen = length $ maximumBy (comparing length) ss | |
| histogram :: [Integer] -> String | |
| histogram [] = "" | |
| histogram xs = unlines . transpose . pad $ map (\n -> case lookup n (frequencies xs) of | |
| Just count -> histLine (n, count) | |
| Nothing -> histLine (n, 0)) [0..9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment