我正在嘗試編寫一個程序,使用Python 3.6查找DNA序列中的所有palindromes。我已經有一個工作函數來檢查一個DNA序列是否是一個迴文序列(與迴文一詞有點不同),但我似乎無法得到應該從DNA序列中返回所有迴文序列的功能。目前,只有當DNA序列是迴文序列時,它纔會返回一個列表,而不是DNA序列中的迴文序列。查找所有palindromes的問題
以防萬一:功能檢查序列是否是一個迴文(工作正常):
def checkPalindrome(DNA):
converter = DNA.maketrans("ATGC", "TACG") # finds the complementary strand
complementary_strand = DNA.translate(converter)
# reverses the complementary strand
palindrome_check = complementary_strand[::-1]
if strand == palindrome_check:
return True
else:
return False
,並應找到所有迴文+執行代碼的功能(不工作) :
def allPalindromes(DNA):
left,right = 0,len(DNA)
j = right
palindromes = []
while left < right - 1:
temp = DNA[left:j]
j -= 1
if checkPalindrome(temp):
palindromes.append(temp)
if j < left+2:
left += 1
j = right
return list(set(palindromes))
strand = getInput()
all_palindromes = allPalindromes(strand)
print(all_palindromes)
你的'checkPalindrome'函數有一個錯誤。它不知道'strand'是什麼。或者這是一個全局變量? – Elmex80s
就是這樣!謝謝!我不敢相信我犯了這樣一個愚蠢的錯誤。 – Danielle
不知道您使用的是哪種編輯器,但像PyCharm這樣的編輯器立即亮起這些錯誤。 – Elmex80s