code for Computer Language Shootout

Raymond Hettinger vze4rx4y at verizon.net
Thu Mar 17 07:09:46 CET 2005


> > . import string, itertools, sys
> > .
> > . t = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
> > .                      'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')
> > .
> > . for h,b in itertools.groupby( file(sys.argv[1]), lambda x: x[0] in
> > ">;" ):
> > .     if h:
> > .         print "".join(b),
> > .     else:
> > .         b = "".join(b).translate(t, "\n\r")
> > .         print "\n".join( b[-i:-i-60:-1] for i in xrange(1, len(b),
> > 60) )
> >
>
> I benchmarked this, btw - it ran in the same amount of time as the other
> solution. It does have the advantage of being significantly fewer lines of
> code; I suppose that itertools.groupby, while unexpected to someone from a
> language without such niceties in the standard library =), is a better
> solution than duplicating the code (or the function call) to translate,
> reverse, and format the string.

While the use of groupby() is brilliant, I prefer the previous version as a
demonstration of beautiful, general purpose, plain Python code running at
full-speed.


Raymond Hettinger





More information about the Python-list mailing list