0

所以这是一个家庭作业问题,它让我很困惑。我曾多次尝试编写此代码,但仍然一无所获。问题是找到两条 DNA 链之间最长的公共子序列并打印该序列。这里的任何人都可以帮助我并让我知道该怎么做吗?我已经设置了窗口并使用 find 函数查看字符串在哪里相等,然后设置一个 max 函数只显示最长的。

def main():

s1 = input("Enter first strand: ")
s2 = input("Enter second strand: ")
window = 0
if len(s1) < len(s2):
    window = len(s1)
elif len(s1) > len(s2):
    window = len(s2)
else:
    window = len(s1)
for i in range(window, 1, -1):
    for j in range(0, i, 1):
     if s1.find(s1[i:i + window]) == s2.find(s2[i:i + window]):
        max = a[0][0]
        for i in range(len(a)):
            for j in range(len(a[i]):
                if (a[i][j] > max):
                    max = a[i][j]
                    print ("Common Subsequence: ", max)
     else:
        print ("No Common Sequence Found")

main()
4

1 回答 1

0

假设你得到了等长的股线(因为这是家庭作业,我会让你调整它以获得不等长的股线)

from itertools import zip_longest, chain
def longestCommonSubstring(p1, p2):

    answer = [[0.0 for _ in range(len(p2)+1)] for _ in range(len(p1)+1)]

    for r, row in enumerate(answer[1:], 1):
        for c, _ in enumerate(row[1:], 1):
            if p1[r-1] == p2[c-1]:
                answer[r][c] = answer[r-1][c-1]+1
            else:
                answer[r][c] = 0.0

    return max(chain.from_iterable(matrix))/max(len(p1), len(p2))

希望这可以帮助

编辑:实施find

def myFind(string, substring):
    for i in range(len(string) - len(substring)):
        if string[i] == substring[0]:
            if string[i:i+len(substring)] == substring:
                print substring, "found in", string, "at position", i

如果您不想使用字符串比较:

def myFind(string, substring):
    for i in range(len(string) - len(substring)):
        if string[i] == substring[0]:
            found = True
            for j in range(len(substring)):
                if string[i+j] != substring[j]:
                    found = False
            if found:
                print substring, "found in", string, "at position", i
于 2013-07-30T02:49:01.800 回答