我需要在原谅一定数量的错误(n)的同时,找出A是否是B的子串。A和B都是字符串,n是整数。最大的问题是我的内部while循环。我不知道如何编码它,使它输出我想要的。它应该在B中循环寻找a与n个错误的匹配。如果A是“abcd”,B是“bdefabddghl”,那么输出应该是在位置4处发现A,有1处错误。 这是我现在的密码。我只需要知道如何编写内部while循环。非常感谢您的帮助。你知道吗
def subsequence(A,B,n):
answer = -1 # assume worst case for there-exists type of loop
j = 0 # index b
while j<=len(B)-len(A) and answer==-1: # there exists j in b loop
bx = j # assume a is found in b starting at b[j]
i = 0 # assume best case for a for-all type of loop
axy = n # accumulator for n
while i<len(A) and bx==j and axy > 0: # for all i in a
if A[i] == B[j-i] and axy > 0:
bx = j
axy = n # accumulator for n
if A[i] != B[j-i] and axy > 0:
axy -= 1
i+=1
# while i
j+=1
# while j
end = "A best match with " + str(n-axy) + " errors was found starting at position " + str(bx)."
return end
print subsequence("abcd","bcjabddec",3)
谢谢
您可能应该看看
difflib.get_close_matches
函数。它的作用几乎是一样的。如果您真的想实现它,那么只需构建一个相同长度的所有子序列的列表并按匹配数对它们进行排序,就更容易实现了。你知道吗我假设你正在做这个练习,或者类似的:http://www.cs.hofstra.edu/~cscccl/csc15p/dnalab.txt
我不确定开始的例子是否特别有用。它很难理解,因此很难按照指定的方式进行调整。我想就如何解决这些问题提出一些建议:
你要做的是沿着线B滑动线A,在每个位置测试它的匹配度,记住你找到的最佳匹配。问题中一个容易区分的部分是测量两个字符串在特定对齐方式下的匹配程度。你知道吗
以下是我对解决方案的一般形式的建议:
下面是一些基本测试的样子:
首先找出一个字符串距离的实现,它将通过这些测试。然后开始工作。你知道吗
相关问题 更多 >
编程相关推荐