code for Computer Language Shootout

Jacob Lee jelee2 at uiuc.edu
Wed Mar 16 00:59:43 EST 2005


On Tue, 15 Mar 2005 21:38:48 -0800, Michael Spencer wrote:

> 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')
> 
Good call.

> 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)
> 
> 
That's the immediately obvious solution, but it doesn't actually fulfill
the problem requirements. What if your last line is less than 60
characters long? You no longer will be displaying the input in reverse
order. Otherwise you'd be right - my solution would be unnecessarily
unwieldy (and the problem would be much simpler...) .

-- 
Jacob Lee
jelee2 at uiuc.edu | www.nearestneighbor.net




More information about the Python-list mailing list