The main difference came down to the fact that Katie decided to avoid using the regular expression that was given in the sample, and just parse the lines with
str.split.So without much more talking, here is the winning solution.
import sys
s=sum(([l[0]]*int(l[-1])for l in map(str.split,sys.stdin)if l[-1]!='-'),[])
for x in set(s):print x,s.count(x)
It is also interesting to note how she uses the sum function to append a set of lists together.
Well done katie :D
ReplyDeleteGoes to show when you decide to use regex for one problem, you just end up with two problems ;-)
Win! :D
ReplyDeleteJust don't try it with big numbers. :P
Well done Katie! However, this is what I achived in Ruby in 63 chars!
ReplyDeletem=Hash.new(0);STDIN.map{|l|m[l[/^\S+/]]+=l[/\d+\s+$/].to_i};p m
:)
Leaving out regexes is just not cool:
ReplyDeleteimport sys,re
d={}
exec(re.sub('([^\s]*).*?([^\s]*?\n)',r'd["\1"]=d.get("\1",0)+\2',sys.stdin.read()))
print d
(ok, I'm not doing as much error checking)