# Hamming distance between two DNA strings

By definition from Wikipedia, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it is the number of substitutions required to transform one string into another. Given two strings of equal length, compute the Hamming distance.

So to find the how many differences there are between two strings, say we have two DNA strings A and B:

A = TGACCCGTTATGCTCGAGTTCGGTCAGAGCGTCATTGCGAGTAGTCGTTTGCTTTCTCAAACTCC
B = GAGCGATTAAGCGTGACAGCCCCAGGGAACCCACAAAACGTGATCGCAGTCCATCCGATCATACA


Throw them to two arrays A and B. Therefore, If

(= count 0)
(cond
[(symbol=? A[1] B[1]) /do nothing just move on to the next pair of string]
[else (add1 count)] ;; counter:= counter + 1;


output would be the counter.

Is this the right logic flow to go with this problem? Any suggestion to write this on python or Scheme?

• I think for the programming side of things, this question should be asked (at least in part) in stackoverflow.com – Behzad Rowshanravan Oct 27 '14 at 22:55
• This question does fit into bioinformatics, which is on-topic here. That said, it really has nothing to do with DNA and you might get a better response at stackoverlow. – canadianer Oct 28 '14 at 3:21
• By the way, this sounds a lot like a homework question and there is a tag for that here. – skymningen Oct 30 '14 at 7:30

Your logic on the problem seems correct. The wikipedia article about the Hamming distance describes both a python and a C implementations. The python version assumes two strings of equal length:

def hamming_distance(s1, s2):
#Return the Hamming distance between equal-length sequences
if len(s1) != len(s2):
raise ValueError("Undefined for sequences of unequal length")
return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))