Yolanda Hui
Yolanda Hui

Reputation: 97

Why is my Python code for DNA pattern not giving the right output?

If I input "ATACTCGTCGTCGATCGATACTCGTCTGTCGTCGAGTCGTTCGTCTCGTC" as the dna, and "TCGTC" as the pattern, it should output

____ * __ * _____________ * ______ * ___________ * ____ * ____ where it marks a star at the start position of the overlapping pattern.

enter image description here

But my code is not giving me the correct output, why?

Here's my code:

def printMatch(dna,pattern):
    for i in range(0,len(dna)):
      if dna[i:len(pattern)]!=pattern:
        dna+="_"
      else:
        dna+="*"
    print(dna)

def main():
    dna=input()
    pattern=input()
    printMatch(dna,pattern)

main()

Upvotes: 0

Views: 77

Answers (1)

BoarGules
BoarGules

Reputation: 16942

The problem is here: dna[i:len(pattern)]. The value after the : is the ending index, not the length of the substring. Do this instead: dna[i:i+len(pattern)].

Upvotes: 3

Related Questions