code for Computer Language Shootout

Michael Spencer mahs at telcopartners.com
Wed Mar 16 00:38:48 EST 2005


Jacob Lee wrote:
> There are a bunch of new tests up at shootout.alioth.debian.org for which
> Python does not yet have code. I've taken a crack at one of them, a task
> to print the reverse complement of a gene transcription. Since there are a
> lot of minds on this newsgroup that are much better at optimization than
> I, I'm posting the code I came up with to see if anyone sees any
> opportunities for substantial improvement. Without further ado:
> 
> table = string.maketrans('ACBDGHK\nMNSRUTWVY', 'TGVHCDM\nKNSYAAWBR')
> 
string.translate is a good idea.  Note you can handle upper-casing and 
translation in one operation by adding a mapping from lower case to the 
complement i.e.,

table = string.maketrans('ACBDGHK\nMNSRUTWVYacbdghkmnsrutwvy',
                          'TGVHCDM\nKNSYAAWBRTGVHCDMKNSYAAWBR')

> def show(s):
>     i = 0
>     for char in s.upper().translate(table)[::-1]:


>         if i == 60:
>             print
>             i = 0
>         sys.stdout.write(char)
>         i += 1
>     print


> def main():
>     seq = ''
>     for line in sys.stdin:
>         if line[0] == '>' or line[0] == ';':
>             if seq != '':
>                 show(seq)
>                 seq = ''
>             print line,
>         else:
>             seq += line[:-1]
>     show(seq)
> 
> main()
> 

This looks unwieldy - especially writing to sys.stdout oe character at a time. 
I may not have understood the spec exactly, but isn't this the same as:

for line in sys.stdin:
     if line and (line[0] == ">" or line[0] == ";"):
         print line
     else:
         print line.translate(table)

HTH

Michael




More information about the Python-list mailing list