I am doing a bioinformatics course and I am trying to write a function to find all occurrences of a substring within a string.
JavaScript
x
21
21
1
def find_match(s, t):
2
"""Returns a list of all positions of a substring t in string s.
3
4
Takes two arguments: s & t.
5
"""
6
occurrences = []
7
for i in range(len(s)-len(t)+1): # loop over alignment
8
match = True
9
for j in range(len(t)): # loop over characters
10
if s[i+j] != t[j]: # compare characters
11
match = False # mismatch
12
break
13
if match: # allchars matched
14
occurrences.append(i)
15
16
return(occurrences)
17
18
19
print(find_match("GATATATGCATATACTT", "ATAT")) # [1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 9, 9, 9, 9, 11, 11, 11, 13]
20
print(find_match("AUGCUUCAGAAAGGUCUUACG", "U")) # [1, 4, 5, 14, 16, 17]
21
The output above should exactly match the following:
[2, 4, 10]
[2, 5, 6, 15, 17, 18]
How can I fix this? Preferably without using regular expressions.
Advertisement
Answer
It looks like you badly indented the code, the
JavaScript
1
2
1
if match:
2
has to be outside of the inner cycle.
JavaScript
1
21
21
1
def find_match(s, t):
2
"""Returns a list of all positions of a substring t in string s.
3
4
Takes two arguments: s & t.
5
"""
6
occurrences = []
7
for i in range(len(s)-len(t)+1): # loop over alignment
8
match = True
9
for j in range(len(t)): # loop over characters
10
if s[i+j] != t[j]: # compare characters
11
match = False # mismatch
12
break
13
if match: # <--- This shouldn't be inside the inner for cycle
14
occurrences.append(i + 1)
15
16
return occurrences
17
18
19
print(find_match("GATATATGCATATACTT", "ATAT")) # [1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 9, 9, 9, 9, 11, 11, 11, 13]
20
print(find_match("AUGCUUCAGAAAGGUCUUACG", "U")) # [1, 4, 5, 14, 16, 17]
21