Last active
December 11, 2015 09:48
-
-
Save blerou/4581930 to your computer and use it in GitHub Desktop.
Let's play Haskell! - #7 - simple json & recycled numbers solution
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 MyJson where | |
import Data.List | |
data JValue = JString String | |
| JBool Bool | |
| JNumber Double | |
| JNull | |
| JList [JValue] | |
| JObject [(String, JValue)] | |
instance Show JValue where | |
show = renderJValue | |
renderJValue :: JValue -> String | |
renderJValue JNull = "null" | |
renderJValue (JBool True) = "true" | |
renderJValue (JBool False) = "false" | |
renderJValue (JString s) = show s | |
renderJValue (JNumber n) = show n | |
renderJValue (JList vs) = "[" ++ listItems ++ "]" | |
where listItems = intercalate ", " $ map renderJValue vs | |
renderJValue (JObject pairs) = "{" ++ objectItems ++ "}" | |
where objectItems = intercalate ", " $ map pairToString pairs | |
pairToString (key, jsonValue) = show key ++ ": " ++ renderJValue jsonValue |
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
4 | |
1 9 | |
10 40 | |
100 500 | |
1111 2222 |
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
0 | |
3 | |
156 | |
287 |
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
rotate :: String -> [String] | |
rotate l = rotate_recursive l [] | |
rotate_recursive :: String -> String -> [String] | |
rotate_recursive [] list = [] | |
rotate_recursive list1 list2 = | |
let (h1:t1) = list1 | |
in [list1 ++ list2] ++ rotate_recursive t1 (list2 ++ [h1]) | |
-- http://code.google.com/codejam/contest/1460488/dashboard#s=p2 | |
is_recycled :: String -> String -> Bool | |
is_recycled l1 l2 = length [ x | x <- rotate l1, x /= l1, x == l2] >= 1 | |
recycled_numbers :: Int -> Int -> Int | |
recycled_numbers from to = length [ (x,y) | | |
x <- [from..to], y <- [from..to], x < y, is_recycled (show x) (show y) ] | |
main :: IO () | |
main = interact solve | |
solve :: String -> String | |
solve cases = unlines $ map solveCase $ drop 1 $ lines cases | |
solveCase :: String -> String | |
solveCase s = show $ recycled_numbers (read from) (read to) | |
where [from, to] = words s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment