code for Computer Language Shootout

Steven Bethard steven.bethard at gmail.com
Wed Mar 16 07:45:30 CET 2005


Jacob Lee wrote:
> So here's a tentative contest version of the code:
> 
> import sys
> import string
> 
> def show(seq, table=string.maketrans('ACBDGHK\nMNSRUTWVYacbdghkmnsrutwvy',
>                                      'TGVHCDM\nKNSYAAWBRTGVHCDMKNSYAAWBR')):
>      seq = seq.translate(table)[::-1]
>      for i in range(0, len(seq), 60):
>          print seq[i:i+60]
> 
> def main():
>      seq = []
>      for line in sys.stdin:
>          if line[0] in ';>':
>              show(''.join(seq))
>              print line,
>              del seq[:]
>          else:
>              seq.append(line[:-1])
>      show(''.join(seq))
> 
> main()

Looks pretty good.  (And yes, I definitely prefer the unaliased ''.join 
and seq.append for readability's sake.  Glad to know they try to grade 
on that too.) =)

Only one other suggestion: "range" in the show function should probably 
be "xrange".  "range" is going to create an actual list of however many 
integers, while "xrange" will just create the integers as needed. 
"xrange" thus will be more memory friendly. (It's also occasionally 
faster, though this depends a lot on the rest of the code).

STeVe



More information about the Python-list mailing list