0

My dna program always returns no match and after debugging it I've pin pointed the problem to the counter not updating correctly. My program finds the correct number of matches, which I've confirmed by printing them out, but the counter doesn't increment itself for some reason.

for str in header[1:]:# look at data for first str
        for char in range(len(tf)): #looking at every char for the length of the text file
            if str == tf[char:char+len(str)]: #compare the current str against the next set of values that are the same length as the str
                cc += 1 #if its a match add to the counter
                if char + len(str) < len(tf):
                    char += len(str)-1 # subtract 1 since it already iterates by 1
                #example we start at 0 and its a match so move it up 4 and well start at index 4 next, which is the next set to test
                print(str)
            else: #if its not a match
                if cc > tmax: #if the current count is larger then
                    tmax = cc #change the temp max
                cc = 0 #reset the count
        print(tmax)
        Maxlist.append(tmax)#set the current index to the max we found, appending it on since the list wasnt predefined
        tmax = 0 #reset the max for the next str
        cc = 0 #reset the count

Printing in different parts of the code I can get results like this, and I can see that before tmax is appended to the list it's only 1, despite printing the match in the same section where the counter should have increased by 1:

AGATC
AGATC
AGATC
AGATC
1
AATG
1
TATC
TATC
TATC
TATC
TATC
1
No Match
[1, 1, 1]

So my code can correctly find the correct amount of sequential strs to what the first check is looking for, i.e. Bob, but the counter remains at 1 no matter how many correct sequences it finds. What am I missing here? Also if it matters, the output doesn't change if I increment char with char += len(str)-1 or char += len(str), I thought I'd need to subtract 1 since the for loop already increments char by 1 on it's own, but it doesn't make any difference.

1 Answer 1

1

There's a subtle bug in here:

    for char in range(len(tf)):
        # ...
        char += len(str)-1

Let's say range(len(tf) = [0, 1, 2, 3, 4] and len(str) = 3. What happens if we match while char == 0 is that we will update it using char += ... to char == 2. But then the for-loop goes into the next iteration and resets it to char == 1, because that is the next element in the list returned by range(...).

So for-loops in Python (which really are for-each loops) are subtly different from C-style loops.

The solution here is to replace the for-loop with a while-loop so the counting-up does not get "erased" by getting overwritten with the next element from the range(...).

1
  • Thank you very much for this, I've read into this further and it seems the for loop will evaluate the range only once and not on every iteration like we're used to seeing in C. I've fixed this with a while loop. Now it's on to debug the matching X_X
    – Mike S
    Commented Jun 5, 2020 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .