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 Marcelo_Ozorio
def checkio(n, m):
if n > m:
n = bin(n)
m = bin(m)
n_list = n[2:]
m_list = m[2:]
m_list = m_list.zfill(len(n_list))
else:
n = bin(n)
m = bin(m)
n_list = n[2:]
m_list = m[2:]
n_list = n_list.zfill(len(m_list))
final = []
for i in range(len(n_list)):
if n_list[i] == m_list[i]:
final.append(0)
else:
final.append(1)
return sum(final)
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"
Aug. 2, 2018
Comments: