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 Daniel_Cieslinski
def checkio(n, m):
nbin=bin(n)[2:] # Converting to binary
mbin=bin(m)[2:]
nbin=nbin.zfill(max(len(nbin),len(mbin))) # Adding 0's to de left
mbin=mbin.zfill(max(len(nbin),len(mbin)))
hamming=0
for idx in range(0,len(mbin)):
if mbin[idx]!=nbin[idx]: hamming+=1 # Counting 1's in result
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"
Oct. 12, 2017