Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First1 solution in Clear category for The Hamming Distance by Krzysztof__wierblewski
def checkio(n, m):
def fnConvert(iLeft):
itemp = iLeft
binary = ""
while iLeft != 0:
if iLeft == 1:
binary = binary + "1"
break
else:
itemp = iLeft % 2
binary = binary + str(itemp)
iLeft = iLeft - (iLeft/2)
iLeft = int(iLeft)
return binary
def fnHammingDistance(binN, binM):
iLengthM = len(binM)
iLengthN = len(binN)
iHdistance = 0
#dwdwq
if iLengthN >= iLengthM:
iterator = iLengthN
other = iLengthM
mainString = binN
secondString = binM
else:
iterator = iLengthM
other = iLengthN
mainString = binM
secondString = binN
i = 0
while i < iterator:
if i < other:
if mainString[i] == "1" and mainString[i] != secondString[i]:
iHdistance = iHdistance + 1
elif mainString[i] == "0" and secondString[i] == "1":
iHdistance = iHdistance + 1
else:
if mainString[i] == "1":
iHdistance = iHdistance + 1
i=i+1
return iHdistance
result = fnHammingDistance(fnConvert(n),fnConvert(m))
print(result)
return result
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. 30, 2017