i'm just learning python 3 now. '''It's ask the user for two string and find the Hamming distance between the strings.Input sequences should only include nucleotides ‘A’, ’T’, ‘G’ and ‘C’. Program should ask the user to reenter the sequence if user enter an invalid character.Program should be able to compare the strings are of same length. If the strings are not of the same length program should ask the user to enter the strings again.User should be able to enter upper, lower or both cases as an input '''
The program should print the output in the following format:
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.
What I already try below, but could not get answer.
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
Example: Lets say i have 2 strings of equal length "ABCD" and "ACCC". In this example first and second last are same in both string so you need to substitute 2 character in 2nd to make it same hence, hamming distance is 2 in this case. so total hamming distance is 1 + 1 = 2.
The number of positions that two codewords of the same length differ is the Hamming distance. 27. In case of DNA sequences, we define this distance as the number of bases by which they differ.
The Hamming distance d(10101, 11110) is 3 because 10101 ⊕ 11110 is 01011 (three 1s).
There are several ways to measure the distance between two strings. The simplest one is to use hamming distance to find the number of mismatch between two strings. However, the two strings must have the same length.
the line indent error: 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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With