python program runtime taking so long/won’t end

so i’m doing the dna problem for cs50 wherein i have to count the number of times a STR repeats in a dna sequence. i had an idea on how to solve the problem so i took one of the data and ran my code but the problem is that the program doesn’t end and keeps running i think it has been about 10 minutes now from when i started the program and it still like this. here’s the code:

text="AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG"
length=len(text)
AGAT=0
tmp=0
for i in range(length):
    while text[i]=="A" and text[i+1]=="G" and text[i+2]=="A" and text[i+3]=="T":
        tmp+=1
    if tmp>AGAT:
        AGAT=tmp
    else:
        AGAT=AGAT
print("done")

Answer

As mentioned in the comments there is an infinite loop in your while loop, you could just remove it and choose to use a sliding window technique where you go over the text looking at neighbouring slices of 4 adjacent characters at a time:

text = "AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG"
search_seq = "AGAT"
count = 0
for i in range(len(text) - len(search_seq) + 1):
    if text[i:i+len(search_seq)] == search_seq:
        count += 1
print(f"Sequence {search_seq} found {count} times")

Output:

Sequence AGAT found 5 times

Leave a Reply

Your email address will not be published. Required fields are marked *