我现在只是在学习 python 3。
'''它要求用户输入两个字符串并找到字符串之间的汉明距离。输入序列应该只包含核苷酸“A”、“T”、“G”和“C”。如果用户输入了无效字符,程序应该要求用户重新输入序列。程序应该能够比较相同长度的字符串。如果字符串的长度不同,程序应该要求用户再次输入字符串。用户应该能够输入大写、小写或两种情况作为输入 '''
该程序应按以下格式打印输出:
please enter string one: GATTACA
please enter string two: GACTATA
GATTACA
|| || |
GACTATA
The hamming distance of sequence GATTACA and GACTATA is 2
So the Hamming distance is 2.
我已经在下面尝试过,但无法得到答案。
def hamming_distance(string1, string2):
string1 = input("please enter first sequence")
string2 = input("please enter second sequence")
distance = 0
L = len(string1)
for i in range(L):
if string1[i] != string2[i]:
distance += 1
return distance
最佳答案
行缩进错误:L = len(strings1)
def hamming_distance(s1, s2):
if len(s1) != len(s2):
raise ValueError("Strand lengths are not equal!")
return sum(ch1 != ch2 for ch1,ch2 in zip(s1,s2))
关于python - 找到两个 DNA 串之间的汉明距离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48799955/