Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Hamming Distance by krk05
def dec_bin(dec):
bin = ""
while(dec > 0):
bin = str(dec%2) + bin
dec//=2
return bin
def checkio(n, m):
dl = 0
wynik = 0
binn = dec_bin(n)
binm = dec_bin(m)
if len(binm)>len(binn):
dl = len(binm)
binn = binn.zfill(dl)
elif len(binn) > len(binm):
dl = len(binn)
binm = binm.zfill(dl)
elif len(binn) == len(binm):
dl = len(binn)
for i in range (dl):
if binm[i] == '0' and binn[i]== '1':
wynik +=1
elif binm[i] == '1' and binn[i] == '0':
wynik += 1
return wynik
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. 17, 2018