Hamming Distance

Foundational Python Foundations Hamming Distance Point Mutation Comparison
Significance:

The Hamming distance between two equal-length sequences counts point mutations, which is the
simplest possible molecular clock: more substitutions implies more evolutionary time since
divergence. It underpins SNP calling and read-error estimation, and it's the base case that
edit-distance and alignment algorithms generalise.

Statement

Given two DNA strings of equal length, count the positions at which their symbols differ.
This is the Hamming distance.

The two strings arrive on two separate lines. Print a single integer on one line.

Input — read from standard input
Variable Type Description
s
line 1
str The first DNA string
1 <= len(s) <= 1000, uppercase A, C, G, T only
t
line 2
str The second DNA string
len(t) == len(s)

These variables are already read for you in the starter code on the right.

Output

int the number of positions at which s and t differ

Sample Cases
Sample 1
Input
GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
Expected Output
7
Seven of the seventeen positions carry different bases.
Sample 2
Input
ACGT
ACGT
Expected Output
0
Identical strings have distance 0.

Submit also runs your code against 4 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • 1 <= length(s) <= 1000
  • length(t) == length(s) — the strings are always the same length
  • Uppercase A, C, G, T only
Further Reading
  • sum(1 for a, b in zip(s, t) if a != b) in Python.
  • In R, sum(strsplit(s, "")[[1]] != strsplit(t, "")[[1]]).

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: s (str), t (str)
Next Problem
Sequence Report Card