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_Dziedzic
def checkio(n, m):
Bin1 = bin(n)
Bin2 = bin(m)
Sum = 0
Bin1 = Bin1.lstrip("0b")
Bin2 = Bin2.lstrip("0b")
Difference = abs(len(Bin1) - len(Bin2))
if(len(Bin1) > len(Bin2)):
for x in range(0,Difference):
Bin2 = "0" + Bin2
else:
for x in range(0,Difference):
Bin1 = "0" + Bin1
for z in range(0, len(Bin1)):
Sum = Sum + abs(int(Bin1[z]) - int(Bin2[z]))
return Sum
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"
Oct. 27, 2016