Approximate pattern matching?
<p>I am trying to write code for Approximate Pattern Matching which is as below:</p>

<pre><code>def HammingDistance(p, q):
    d = 0
    for p, q in zip(p, q): # your code here
        if p!= q:
            d += 1
    return d
Pattern = "ATTCTGGA"
Text = "CGCCCGAATCCAGAACGCATTCCCATATTTCGGGACCACTGGCCTCCACGGTACGGACGTCAATCAAAT"
d = 3
def ApproximatePatternMatching(Pattern, Text, d):
    positions = [] # initializing list of positions
    for i in range(len(Text) - len(Pattern)+1):
        if Pattern == Text[i:i+len(Pattern)]:
            positions.append(i)# your code here
    return positions
print (ApproximatePatternMatching(Pattern, Text, d))
</code></pre>

<p>I keep getting the following error:
Failed test #3. You may be failing to account for patterns starting at the first index of text.</p>

<p>Test Dataset:</p>

<pre><code>GAGCGCTGG
GAGCGCTGGGTTAACTCGCTACTTCCCGACGAGCGCTGTGGCGCAAATTGGCGATGAAACTGCAGAGAGAACTGGTCATCCAACTGAATTCTCCCCGCTATCGCATTTTGATGCGCGCCGCGTCGATT
2
</code></pre>

<p>Your output:</p>

<pre><code>['[]', '0']
</code></pre>

<p>Correct output:</p>

<pre><code>['0', '30', '66']
</code></pre>

<p>Can not figure out what I am doing wrong as I am trying to learn python so don't have any idea about programming. Need help?</p>

<p>I'm unsure why you're getting an empty list as one of your outputs - when I run your code above I only get [0] as the print out.</p>

<p>Specifically, your code at present only checks for an exact character substring match, without using the hamming distance definition you also included. </p>

<p>The following should return the result you expect:</p>

<pre><code>Pattern = "GAGCGCTGG"
Text = "GAGCGCTGGGTTAACTCGCTACTTCCCGACGAGCGCTGTGGCGCAAATTGGCGATGAAACTGCAGAGAGAACTGGTCATCCAACTGAATTCTCCCCGCTATCGCATTTTGATGCGCGCCGCGTCGATT"
d = 3

def HammingDistance(p, q):
    d = 0
    for p, q in zip(p, q): # your code here
        if p!= q:
            d += 1
    return d

def ApproximatePatternMatching(Pattern, Text, d):
    positions = [] # initializing list of positions
    for i in range(len(Text) - len(Pattern)+1):
        # and using distance &lt; d, rather than exact matching
        if HammingDistance(Pattern, Text[i:i+len(Pattern)]) &lt; d:
            positions.append(i)
    return positions

print (ApproximatePatternMatching(Pattern, Text, d))
</code></pre>

python