[Tutor] function help

James Reynolds eire1130 at gmail.com
Mon Feb 7 20:00:26 CET 2011


I'm not sure of the answer to your question, because i'm not sure I
understand it. Perhaps you could rephrase it?

But, there is some other stuff I noticed though:

def padWithGaps(seq):

    for letter in seq:

       letter="-"

       line=len(seq)

       dashline=line*letter

    return dashline



This function is fairly inefficient. Let's say you pass the function a
sequence three items long something like a_list = ['a','b','c']. Your
telling Python to create a dashed line three dashes long
three separate times. So, you do it for the 'a' part of the sequence, the
'b' and the 'c'. You can condense this to something like:

def padWithGaps(seq):

    lent = len(seq)

    return lent*'-'


And you really don't need to have a separate function for that, as you can
just do it on the fly

Another thing, whenever your taking a slice of a list and the first argument
is 0, you can just omit that. For example, from my list a_list example above
a_list[0:2] is equivalent to a_list[:2]

As for the rest of them, what you could do is pass each function data that
you would expect to be passed under normal operation (assuming this is
possible of course) and instead of using return use print instead and see if
the results are what you expect.

What I mean is, let's say I pass the results from the padWithGaps (the
results of which I call dashline to be consistent) to the next function:

def replace(someString,position,letter):

    first=someString[0:position]

    second=letter

    third=someString[position+1:len(someString)]

    newString=first+second+third

    return newString


So, I can do something like z = replace(dashline,0,'a') and then print z.
What the results should be is "a--".


def findScore(first,second):

    count=0

    for position in range(0,len(first)):

         if first[position]==second[position]:

             count=count+1

             position=position+1

    return count


With this function here, there is a few things you can do to optimize and
clean up clutter as well. I only point this out because it pays huge
dividends in the long run (or it has for me at least). The first thing
though is that you don't need to have the line position=position+1. Python
will go to the next item in the list "position" whether you tell it to or
not. So below is the same thing but without the position +=1 line:



def findScore(first,second):

    count=0

    for position in range(0,len(first)):

         if first[position]==second[position]:

             count=count+1

    return count


The other thing I would suggest is to use enumerate. Enumerate is your
friend. And the last thing I would suggest is whenever you want to do
something like a = a+1, you can just do a +=1.


> def findScore2(first,second):

    count=0

    for i, position in enumerate(first):

         if position==second[i]:

             count+=1

    return count


enumerate returns both the next item in the list and the position of that
item in the list (in this case, I called that variable i). So you will find
that if you run findScore2 you will have the same results as findScore. Or
at least you should ;)

On Mon, Feb 7, 2011 at 11:45 AM, Ashley F <aflint310 at yahoo.com> 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
>
> def replace(someString,position,letter):
>     first=someString[0:position]
>     second=letter
>     third=someString[position+1:len(someString)]
>     newString=first+second+third
>     return newString
>
> ##findScore("MPFVS","MS-V-") would return a score of 2
> def findScore(first,second):
>     count=0
>     for position in range(0,len(first)):
>          if first[position]==second[position]:
>              count=count+1
>              position=position+1
>     return count
>
> #shorter is a 3 amino acid sequence
> ##longer is any length sequence greater than 3
> ###i=first amino acid; j=second amino acid; k=third amino acid
> def findAlignment(shorter,longer):
>      for i in range(0,len(longer)-2):
>          for j in range(1,len(longer)-1):
>               for k in range(2,len(longer)):
>                    dashline=padWithGaps(longer)
>                    nextLine=replace(dashline,i,shorter[0])
>                    nextNext=replace(nextLine,j,shorter[1])
>                    alignment=replace(nextNext,k,shorter[2])
>                    score=findScore(longer,alignment)
>            ####don't know what to do here
>       print longer
>       print alignment
>       print "Score = " + str(score)
>
> 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"
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110207/2a685b5b/attachment-0001.html>


More information about the Tutor mailing list