[Tutor] function help
Steven D'Aprano
steve at pearwood.info
Mon Feb 7 23:36:02 CET 2011
Ashley F wrote:
> ok...here's the function I've written so far.
>
> def padWithGaps(seq):
> for letter in seq:
> letter="-"
> line=len(seq)
> dashline=line*letter
> return dashline
I don't think that's a useful function. It seems to do a HUGE amount of
work that just keeps getting thrown away. Let's say seq = "GACT", this
function will do the following:
letter = "G"
letter = "-"
line = 4 # len of seq
dashline = "----"
letter = "A"
letter = "-"
line = 4
dashline = "----"
letter = "C"
letter = "-"
line = 4
dashline = "----"
letter = "T"
letter = "-"
line = 4
dashline = "----"
It does everything four times. Now imagine that seq is a million
characters long instead of four! This will do the job *much* faster:
def padWithGaps(seq):
return "-" * len(seq)
*Much* faster, much easier to read.
[...]
> I don't know what to do at the end of my loop but what I'm trying to do in pseudocode is:
> "if that alignment has the best score seen so far
> save the score and the alignment
> print the best score and the best alignment"
You need to store the current best score and the current best alignment.
Then each time around the innermost loop, you need to calculate the
score and alignment (as you already do), and compare them to the best
seen so far. If they are worse or equal, you don't need to do anything,
just go on to the next loop as normal. But if they are better, then you
need to update the best score and alignment, and print them.
if score > best_score:
best_score = score
best_alignment = alignment
print 'best seen so far is', score, alignment
Does that help? Try writing the code, and if you still can't get it
working, ask again.
--
Steven
More information about the Tutor
mailing list