我发现了一个用python编写的有关Naive字符串算法的非常有用的代码,但它似乎没有运行什么问题?
def naive(p, t):
occurrences = []
for i in range(len(t) - len(p) + 1): # loop over alignments
match = True
for j in range(len(p)): # loop over characters
if t[i+j] != p[j]: # compare characters
match = False # mismatch; reject alignment
break #goes back to the outer loop and start the next step of looping in the alignment
if match: #if match remained true
occurrences.append(i) # all chars matched; record
return occurrences
naive("asge","asgefjlso")
#运行#但它不运行且不显示任何内容
也可以有人给我解释一下,如果t [i + j]!= p [j]意味着做什么吗?
答案 0 :(得分:0)
我假设您已按照与上述相同的格式执行代码,然后由于缩进问题而无法运行,带有正确缩进的正确代码如下
def naive(p, t):
occurrences = []
for i in range(len(t) - len(p) + 1): # loop over alignments
match = True
for j in range(len(p)): # loop over characters
if t[i+j] != p[j]: # compare characters
match = False # mismatch; reject alignment
break #goes back to the outer loop
# and start the
#next
#step of looping in the alignment
if match: #if match remained true
occurrences.append(i) # all chars matched; record
return occurrences