Note that there are some explanatory texts on larger screens.

plurals
  1. POpython recursion with bubble sort
    primarykey
    data
    text
    <p>So, i have this problem where i recieve 2 strings of letters ACGT, one with only letters, the other contain letters and dashes "-".both are same length. the string with the dashes is compared to the string without it. cell for cell. and for each pairing i have a scoring system. i wrote this code for the scoring system: for example: dna1: -ACA dna2: TACG the scoring is -1. (because dash compared to a letter(T) gives -2, letter compared to same letter gives +1 (A to A), +1 (C to C) and non similar letters give (-1) so sum is -1.</p> <pre><code>def get_score(dna1, dna2, match=1, mismatch=-1, gap=-2): """""" score = 0 for index in range(len(dna1)): if dna1[index] is dna2[index]: score += match elif dna1[index] is not dna2[index]: if "-" not in (dna1[index], dna2[index]): score += mismatch else: score += gap </code></pre> <p>this is working fine.</p> <p>now i have to use recursion to give the best possible score for 2 strings. i recieve 2 strings, they can be of different sizes this time. ( i cant change the order of letters). so i wrote this code that adds "-" as many times needed to the shorter string to create 2 strings of same length and put them in the start of list. now i want to start moving the dashes and record the score for every dash position, and finally get the highest posibble score. so for moving the dashes around i wrote a litle bubble sort.. but it dosnt seem to do what i want. i realize its a long quesiton but i'd love some help. let me know if anything i wrote is not understood.</p> <pre><code>def best_score(dna1, dna2, match=1, mismatch=-1, gap=-2,\ score=[], count=0): """""" diff = abs(len(dna1) - len(dna2)) if len(dna1) is len(dna2): short = [] elif len(dna1) &lt; len(dna2): short = [base for base in iter(dna1)] else: short = [base for base in iter(dna2)] for i in range(diff): short.insert(count, "-") for i in range(diff+count, len(short)-1): if len(dna1) &lt; len(dna2): score.append((get_score(short, dna2),\ ''.join(short), dna2)) else: score.append((get_score(dna1, short),\ dna1, ''.join(short))) short[i+1], short[i] = short[i], short[i+1] if count is min(len(dna1), len(dna2)): return score[score.index(max(score))] return best_score(dna1, dna2, 1, -1, -2, score, count+1) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload