Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Hamming Distance by Krzysztof.Sawarzynski
def checkio(n, m):
n = list(bin(n))[2:]
m = list(bin(m))[2:]
diff = 0
if len(n) > len(m):
bigger = n
smaller = m
else:
bigger = m
smaller = n
while len(bigger) > 0:
if len(smaller) > 0:
if smaller[-1] != bigger[-1]:
diff += 1
smaller.pop()
else:
if bigger[-1] != '0':
diff += 1
bigger.pop()
return diff
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"
Jan. 19, 2017