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:


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

(= count 0)
   [(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?

  • 2
    $\begingroup$ I think for the programming side of things, this question should be asked (at least in part) in stackoverflow.com $\endgroup$ Commented Oct 27, 2014 at 22:55
  • 1
    $\begingroup$ 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. $\endgroup$
    – canadianer
    Commented Oct 28, 2014 at 3:21
  • $\begingroup$ By the way, this sounds a lot like a homework question and there is a tag for that here. $\endgroup$
    – skymningen
    Commented Oct 30, 2014 at 7:30

1 Answer 1


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))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .