I found an interesting algorithm to calculate hamming distance on this site:
def hamming2(x,y):
"""Calculate the Hamming distance between two bit strings"""
assert len(x) == len(y)
count,z = 0,x^y
while z:
count += 1
z &= z-1 # magic!
return count
The point is that this algorithm only works on bit strings and I'm trying to compare two strings that are binary but they are in string format, like
'100010'
'101000'
How can I make them work with this algorithm?
To calculate the Hamming distance, you simply count the number of bits where two same-length messages differ. An example of Hamming distance 1 is the distance between 1101 and 1001 . If you increase the distance to 2 , we can give as an example 1001 and 1010 .
In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.
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 Hamming distance d(10101, 11110) is 3 because 10101 ⊕ 11110 is 01011 (three 1s).
Hamming distance is the number of bits positions in which two numbers differ while comparing two binary strings of equal length. Example:a=3 ,b=5 output: 2 Explanation: 3 [0 0 1 1] 5 [0 1 0 1] in index 1 and 2,the value of corresponding bits are different. In this approach, we will use XOR.
Hamming Distance. Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different. The Hamming distance between two strings, a and b is denoted as d (a,b).
Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits that are different at the same position in both numbers. Input: n1 = 9, n2 = 14 Output: 3 9 = 1001, 14 = 1110 No. of Different bits = 3 Input: n1 = 4, n2 = 8 Output: 2
What is Hamming Distance? What is Hamming Distance? Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different. The Hamming distance between two strings, a and b is denoted as d (a,b).
Implement it:
def hamming2(s1, s2):
"""Calculate the Hamming distance between two bit strings"""
assert len(s1) == len(s2)
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
And test it:
assert hamming2("1010", "1111") == 2
assert hamming2("1111", "0000") == 4
assert hamming2("1111", "1111") == 0
If we are to stick with the original algorithm, we need to convert the strings to integers to be able to use the bitwise operators.
def hamming2(x_str, y_str):
"""Calculate the Hamming distance between two bit strings"""
assert len(x_str) == len(y_str)
x, y = int(x_str, 2), int(y_str, 2) # '2' specifies we are reading a binary number
count, z = 0, x ^ y
while z:
count += 1
z &= z - 1 # magic!
return count
Then we can call it as follows:
print(hamming2('100010', '101000'))
While this algorithm is cool as a novelty, having to convert to a string likely negates any speed advantage it might have. The answer @dlask posted is much more succinct.
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