Last active
August 29, 2015 14:02
-
-
Save damncabbage/1a9817a348e8a4e522c0 to your computer and use it in GitHub Desktop.
Haskell UPenn Exercises (http://www.seas.upenn.edu/~cis194/lectures.html)
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 Golf where | |
-- | Foo | |
-- | |
-- >>> skips "ABCD" | |
-- ["ABCD","BD","C","D"] | |
-- | |
-- >>> skips "hello!" | |
-- ["hello!","el!","l!","l","o","!"] | |
-- | |
-- >>> skips [1] | |
-- [[1]] | |
-- | |
-- >>> skips [True,False] | |
-- [[True,False],[False]] | |
-- | |
-- >>> skips [] | |
-- [] | |
skips :: [a] -> [[a]] | |
skips a = | |
map (\n -> every n a) [1..(length a)] | |
where | |
every n xs = case drop (n - 1) xs of | |
(y:ys) -> y : every n ys | |
[] -> [] | |
-- | Local Maxima | |
-- TODO: This is abysmal. | |
-- | |
-- >>> localMaxima [2,9,5,6,1] | |
-- [9,6] | |
-- | |
-- >>> localMaxima [2,3,4,1,5] | |
-- [4] | |
-- | |
-- >>> localMaxima [1,2,3,4,5] | |
-- [] | |
localMaxima :: [Integer] -> [Integer] | |
localMaxima (x:y:z:zs) = (judgeTrio x y z) ++ (localMaxima $ y : z : zs) | |
where | |
judgeTrio a b c = if b > a && b > c then [b] else [] -- TODO: This is actually a terrible version of filter. | |
localMaxima _ = [] |
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
{-# OPTIONS_GHC -Wall #-} | |
module LogAnalysis where | |
import Log | |
-- 1 | |
parseMessage :: String -> LogMessage | |
parseMessage msg = case (words msg) of | |
("I":time:string) -> LogMessage Info (read time) (unwords string) | |
("W":time:string) -> LogMessage Warning (read time) (unwords string) | |
("E":code:time:string) -> LogMessage (Error (read code)) (read time) (unwords string) | |
_ -> Unknown msg | |
parse :: String -> [LogMessage] | |
parse x = (map parseMessage)(lines x) | |
-- 2, 3 | |
insert :: LogMessage -> MessageTree -> MessageTree | |
insert (Unknown _) tree = tree -- Ignore Unknowns. | |
insert msg Leaf = Node Leaf msg Leaf -- Leaf represents an empty node; fill it, stub the children. | |
insert msg (Node left node right) | |
| (ts msg <= ts node) = Node (insert msg left) node right | |
| otherwise = Node left node (insert msg right) | |
where | |
ts (LogMessage _ t _) = t | |
ts _ = 0 | |
build :: [LogMessage] -> MessageTree | |
build [] = Leaf | |
build (x:xs) = insert x (build xs) | |
-- 4 | |
inOrder :: MessageTree -> [LogMessage] | |
inOrder Leaf = [] | |
inOrder (Node l m r) = inOrder l ++ [m] ++ inOrder r | |
-- 5 | |
whatWentWrong :: [LogMessage] -> [String] | |
whatWentWrong xs = (map message (filter important (inOrder (build xs)))) | |
where | |
important (LogMessage (Error sev) _ _) = (sev >= 50) | |
important _ = False | |
message (LogMessage _ _ m) = m | |
message (Unknown _) = "" |
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
-- 1) | |
toDigits :: Integer -> [Integer] | |
toDigits x | |
| x <= 0 = [] | |
| otherwise = toDigits (x `div` 10) ++ [x `mod` 10] | |
-- 2) | |
toDigitsRev :: Integer -> [Integer] | |
toDigitsRev x | |
| x <= 0 = [] | |
| otherwise = [x `mod` 10] ++ toDigitsRev (x `div` 10) | |
-- 3) | |
doubleEveryOther xs = reverse $ doubleEveryOther' $ reverse xs | |
doubleEveryOther' :: [Integer] -> [Integer] | |
doubleEveryOther' [] = [] | |
doubleEveryOther' [x] = [x] | |
doubleEveryOther' (x:(y:zs)) = x : (y * 2) : doubleEveryOther' zs | |
-- 4) | |
sumDigits :: [Integer] -> Integer | |
sumDigits [] = 0 | |
sumDigits (x:xs) | |
| x > 9 = sumDigits ((toDigits x) ++ xs) | |
| otherwise = x + sumDigits xs | |
-- 5) | |
-- Examples: | |
-- >>> validate 4012888888881881 | |
-- True | |
-- >>> validate 4012888888881882 | |
-- False | |
validate :: Integer -> Bool | |
validate x = ((sumDigits (doubleEveryOther (toDigits x))) `mod` 10) == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment