Skip to content

Instantly share code, notes, and snippets.

@dmedvinsky
Created June 3, 2011 11:07
Show Gist options
  • Save dmedvinsky/1006191 to your computer and use it in GitHub Desktop.
Save dmedvinsky/1006191 to your computer and use it in GitHub Desktop.
ios.e-legion.com test task
import Maybe
import Data.List
import Text.XML.Light
main :: IO()
main = readFile "test.xml" >>= putStrLn . solve
solve :: String -> String
solve = formatResult . getTotal . reduce . getList
type Category = String
type Price = Float
getList :: String -> [(Category, Price)]
getList = pairs . items . orders . parseXMLDoc
where name n = QName n Nothing Nothing
orders (Just doc) = findElements (name "order") doc
items = foldl (\ x y -> x ++ findElements (name "item") y) []
pairs = foldl (\ x y -> pair y : x) []
pair x = (fromJust $ findAttr (name "type") x,
read $ fromJust $ findAttr (name "price") x :: Float)
reduce :: [(Category, Price)] -> [(Category, Price)]
reduce = foldl f []
where f xs x@(c, p) = add xs x $ findIndex ((c ==) . fst) xs
add xs x Nothing = x : xs
add xs x (Just n) = let y = xs !! n in (new x y : delete n xs)
new x y = (fst x, snd x + snd y)
delete n xs = let (ys, zs) = splitAt n xs in ys ++ (tail zs)
getTotal :: [(Category, Price)] -> ([(Category, Price)], Price)
getTotal xs = (xs, foldl ((. snd) . (+)) 0 xs)
formatResult :: ([(Category, Price)], Price) -> String
formatResult (xs, t) = f xs ++ "Total: " ++ show t
where f [] = "\n"
f ((c, p):xs) = c ++ ": " ++ show p ++ "\n" ++ f xs
<?xml version="1.0" encoding="UTF-8"?>
<ordersList>
<order id="1" totalPrice="150.00">
<item price="100.00" title="Помидоры" type="Овощи" weight="2" units="kg"/>
<item price="50.00" title="Кока-кола" type="Напитки" quantity="2"/>
</order>
<order id="2" totalPrice="120.00">
<item price="70.00" title="Огурцы" type="Овощи" weight="1" units="kg"/>
<item price="50.00" title="Кока-кола" type="Напитки" quantity="2"/>
</order>
<order id="3" totalPrice="300.00">
<item price="140.00" title="Огурцы" type="Овощи" weight="2" units="kg"/>
<item price="60.00" title="Пепси" type="Напитки" quantity="1"/>
<item price="100.00" title="Вертолёт" type="Игрушки" quantity="1"/>
</order>
</ordersList>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment