I am trying to compute the most repeated STR for each sequence in a dictionary called str_dict.

# compute STRs
i = 0
# stablish length of the sequence
length_seq = len(sequence)
# iterate through all the possibles strs
for s in strs:
    # iterate through the sequence
    while i <= length_seq:
        if sequence[i:i+len(s)] == s:
            counter += 1
            # advance len(s) steps
            i += len(s)
            if counter > str_dict[s]:
                str_dict[s] = counter
            counter == 0
            i += 1

The code should function as intended, but it only computes the first key in the dictionary and the rest still have a value of 0. What am I missing?

  • One note: in your else condition, you evaluate the value of counter rather than assigning it.
    – Sentox
    Nov 25, 2020 at 23:39
  • Good point! Was not able to see that one. However, the output for str_dict(which is the dictionary that stores the count of each str) keeps giving the same output only updating the value for the first key (in this case "AGATC"). Any idea?
    – Jaime
    Nov 26, 2020 at 0:44

1 Answer 1


What is the value of i when the function moves on to the next str?

  • The i pretends to iterate through the sequence(eg. 1.txt). Therefore, what the code intends to do is to advance len(s)(length of the str) steps if there is a match with one of the STRs. Otherwise it will advance i += 1, to the next char in the STR string array. Does that makes sense?
    – Jaime
    Nov 26, 2020 at 1:41
  • 1
    I might have been unnecessarily obtuse here. What I mean is, based on the code shown, when execution returns to the next iteration of the for loop, i is not reset to zero. Hence the while loop will never run again after the first time through.
    – Sentox
    Nov 26, 2020 at 2:12
  • You are right! I realized this before your response after re-checking the loop a thousand times. This little mistakes are making me work triple the time on this pset. Thank you for the help as always!
    – Jaime
    Nov 26, 2020 at 2:43

You must log in to answer this question.

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