^
) to find the bit difference between the two numbers.bin()
to convert the result to a binary string.count()
of str
class to count and return the number of 1
s in it.def hamming_distance(a, b): return bin(a ^ b).count('1')
hamming_distance(2, 3) # 1
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️