# PSET 6 DNA: How to get number of repeats for each value of STR's? [SPOILER: Full code]

I have written a code to count the highest number of consecutive repeats of specific DNA substring sequences in a long string of DNA...or so I thought. While my code can correctly count the longest number of consecutive repeats for the first DNA substring, it fails to do so for the other DNA substrings. I created a empty list, tried to iterate through the DNA string for each key in the list of substrings and get the values corresponding to each sequence, and then added each key's value in the empty list. But only the first one works. How do I account for the other DNA substrings and not just one? Any help would be greatly appreciated! Here is my code:

``````import sys
import csv

if len(sys.argv) != 3:
print("Wrong number of files. Enter correct command-line arguments")
exit(1)

with open (sys.argv[1],'r') as f:
print(strlist)

dna = open(sys.argv[2], "r")
print(sequence_dna)

long_str = []
i = 0
run = 0
long_run = 0
for key in strlist:
while i < len(sequence_dna):
if sequence_dna[i:i+len(key)] == key:
run += 1
if run > long_run:
long_run = run
i += len(key)
elif sequence_dna[i:i+len(key)] != key:
if run > long_run:
long_run = run
run = 0
i += 1
long_str.append(long_run)
print(long_str)

individual = row[0]
values = [int(value) for value in row[1:]]
if values == long_str:
print(individual)
break
elif values != long_str:
print("No match")
break
``````

EDIT: I changed the code slightly and turned `long_str` into a dictionary and changed `.append` to `long_str[key] = long_run`. I added `i = 0, run = 0, and long_run = 0` inside the `for` loop. I also added an extra `for` loop setting `long_str[item] = 0`. I now see that the value added isn't the number of repeats for the first STR substring, but the last one. Now I am even more confused. So in addition to my original question, why does my code only compute the longest number of consecutive repeats for the last STR substring in the dictionary and not for the STR substrings before it? The new additions to my code look like this:

``````long_str = {}

for item in strlist:
long_str[item] = 0

for key in long_str:
i = 0
run = 0
long_run = 0
while i < len(sequence_dna):
if sequence_dna[i:i+len(key)] == key:
run += 1
if run > long_run:
long_run = run
i += len(key)
elif sequence_dna[i:i+len(key)] != key:
if run > long_run:
long_run = run
run = 0
i += 1
long_str[key] = long_run
print(long_str)
``````

The loops are all correct. Put

``````   long_str[key] = long_run
``````

inside the for loop like this:

``````    for key in long_str:
i = 0
run = 0
long_run = 0
while i < len(sequence_dna):
if sequence_dna[i:i+len(key)] == key:
run += 1
if run > long_run:
long_run = run
i += len(key)
elif sequence_dna[i:i+len(key)] != key:
if run > long_run:
long_run = run
run = 0
i += 1
long_str[key] = long_run
``````

Hope this helps. If this works, please give a tick to this answer.