Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The Hamming Distance solution in Clear category for The Hamming Distance by konradgolinski
def checkio(n, m):
n = bin(n)[2:][::-1]
m = bin(m)[2:][::-1]
pow = 1
if(len(n) >= len(m)):
while(1):
if(len(n)> 2**pow):
pow+=1
else:
break;
else:
while(1):
if(len(m)> 2**pow):
pow+=1
else:
break;
bits = 2**pow
for i in range(bits-len(n)):
n += "0"
for i in range(bits-len(m)):
m += "0"
n = n[::-1]
m = m[::-1]
hamming = 0
for i in range(len(n)):
if( n[i] != m[i]):
hamming += 1
print(hamming)
return hamming
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(117, 17) == 3, "First example"
assert checkio(1, 2) == 2, "Second example"
assert checkio(16, 15) == 5, "Third example"
Nov. 21, 2016