Subtotal
Output the subtotal of the amount of each product. Output by changing sequence in ascending order of name.
IN
apple 1200 orange 530 grape 740 orange 310 apple 860 grape 600 orange 250 apple 380
OUT
apple 2440 grape 1340 orange 1090
IN
grape -1000 orange 500 apple 700 banana 480 banana 640 apple 1200 grape 800
OUT
apple 1900 banana 1120 grape -200 orange 500
IN
grape 1500 apple 0 orange -200 grape -1500 orange 200 banana 300 banana -300
OUT
apple 0 banana 0 grape 0 orange 0
import Data.List main=interact$unlines.map(\(n,a)->n++" "++show a).map(\p->(fst$head p,sum$map snd p)).groupBy((.fst).(==).fst).sort.map(\l->let[n,a]=words l in(n,read a)).lines
import Control.Arrow import Data.Function import Data.List r x=(a,read b)where[a,b]=words x a x=map(fst.head&&&foldr1(+).map snd)$groupBy(on(==)fst)$sortBy(on compare fst)x s(a,b)=a++' ':show b main=do{p<-fmap(map r.lines)getContents;mapM_(putStrLn.s)$a p}
import sys lines=map(str,sys.stdin.read().splitlines()) out={} for line in lines: (item,price) = map(str,line.split(' ')) if out.has_key(item): out[item] += int(price) else: out[item] = int(price) for item,price in sorted(out.items(),key=lambda x:x[0]): print item+" "+str(price)