code for Computer Language Shootout

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Mar 16 19:45:53 EST 2005


Michael Spencer's version is nice, this is a bit shortened version. The
main() isn't useful for this very short loop, and you can use shorter
variable names to make lines shorter (this code isn't much readable,
it's just for the Shootout, "production quality" code has probably to
be more readable. Code produced by lot of people of a newsgroup isn't
the normal code usually produced by a single programmer in a limited
amount of time).
I've used file(sys.argv[1]) instead of sys.stdin.


. 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) )

----------------------

The Python Mandelbrot program seems to produce a wrong image:

http://shootout.alioth.debian.org/benchmark.php?test=mandelbrot&lang=python&id=0&sort=fullcpu

----------------------

This is a shorter and faster version of wordfreq:
http://shootout.alioth.debian.org/benchmark.php?test=wordfreq&lang=python&id=0&sort=fullcpu

. import string, sys
.
. def main():
.     f = {}
.     t = " "*65+ string.ascii_lowercase+ " "*6+
string.ascii_lowercase+ " "*133
.
.     afilerl = file(sys.argv[1]).readlines
.     lines = afilerl(4095)
.     while lines:
.         for line in lines:
.             for w in line.translate(t).split():
.                 if w in f: f[w] += 1
.                 else: f[w] = 1
.         lines = afilerl(4095)
.
.     l = sorted( zip(f.itervalues(), f.iterkeys()), reverse=True)
.     print "\n".join("%7s %s" % (f,w) for f,w in l)
.
. main()

----------------------

This is my shorter and faster version of Harmonic (I hope the use of
sum instead of the for is okay for the Shootout rules):
http://shootout.alioth.debian.org/benchmark.php?test=harmonic&lang=python&id=0&sort=fullcpu

import sys
print sum( 1.0/i for i in xrange(1, 1+int(sys.argv[1]) ) )

Bear hugs,
Bearophile




More information about the Python-list mailing list