Jacob Lee wrote:
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...) .

yes it would be, wouldn't it ;-)

How about this then:

basetable = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
                             'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')

from collections import deque
from itertools import islice

def output(seq, linelength = 60):
    if seq:
        iterseq = iter(seq)
        while iterseq:
            print "".join(islice(iterseq,linelength))

def revcomp(input = sys.stdin):
    seqlines = deque()
    for line in input:
        if line[0] in ">;":
            output(seqlines)
            print line,
            seqlines.clear()
        else:
            seqlines.extendleft(line.translate(basetable, "\n\r"))
    output(seqlines)


It would be nice to inline the output function, and re-arrange the iteration so that EOF triggers the same suite as line[0] in ">;"


Michael

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to