Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: code for Computer Language Shootout

Reply
Thread Tools

RE: code for Computer Language Shootout

 
 
Delaney, Timothy C (Timothy)
Guest
Posts: n/a
 
      03-16-2005
Jacob Lee wrote:

>> # alias methods to avoid repeated lookup
>> join = ''.join


I would actually do the alias here sometimes, but give it a
semantically-useful name ...

nosep_join = ''.join

....

Tim Delaney
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      03-17-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/be...0&sort=fullcpu

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

This is a shorter and faster version of wordfreq:
http://shootout.alioth.debian.org/be...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/be...0&sort=fullcpu

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

Bear hugs,
Bearophile

 
Reply With Quote
 
 
 
 
Jacob Lee
Guest
Posts: n/a
 
      03-17-2005
On Wed, 16 Mar 2005 16:45:53 -0800, bearophileHUGS wrote:

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


I don't see what advantage having smaller variable names gives you. IIRC,
they measure lines of code in logical rather than physical lines.

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

> ----------------------
>
> The Python Mandelbrot program seems to produce a wrong image:
>
> http://shootout.alioth.debian.org/be...0&sort=fullcpu
>


It's my understanding that they use an automated diff with the outputs. So
presumably it's generating correct output or it would be listed as
"Error". I haven't actually checked this, so who knows.

> ----------------------
>
> This is a shorter and faster version of wordfreq:
> http://shootout.alioth.debian.org/be...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()
>


Cool. I haven't looked at this one, but why don't you test it against
their sample data, diff it to make sure the output is identical, and send
it to their mailing list .

> ----------------------
>
> 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/be...0&sort=fullcpu
>
> import sys
> print sum( 1.0/i for i in xrange(1, 1+int(sys.argv[1]) ) )
>


Yes, the current Python version is an embarrassment. I was already
planning to send in the one-character patch (s/range/xrange/) when
submitting the reverse-complement code. This version is probably more
efficient than an explicit loop, although I doubt the difference is by
much. I suppose an ounce of profiling is worth a pound of speculation...

--
Jacob Lee
http://www.velocityreviews.com/forums/(E-Mail Removed) | www.nearestneighbor.net

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      03-17-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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Computer Language Shootout igouy@yahoo.com Python 14 11-30-2005 06:23 PM
code for Computer Language Shootout Jacob Lee Python 14 03-29-2005 06:43 PM
The Great Computer Language Shootout (updated ) Isaac Gouy Python 0 08-03-2004 04:23 PM
the new great computer language shootout Isaac Gouy Ruby 0 08-03-2004 04:17 PM
the new great computer language shootout Phil Tomson Ruby 25 06-20-2004 02:26 PM



Advertisments