Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for The Hamming Distance by colinmcnicholl
def checkio(n, m):
"""Input: two positive numbers (N, M) in decimal form.
Function calculates the Hamming distance between the two numbers
in binary form.
Output: the Hamming distance as an integer.
"""
bin_n_chars, bin_m_chars = format(n, '24b'), format(m, '24b')
bin_n_ints = [0 if bin_n_chars[i] == ' ' else int(bin_n_chars[i])
for i in range(len(bin_n_chars))]
bin_m_ints = [0 if bin_m_chars[i] == ' ' else int(bin_m_chars[i])
for i in range(len(bin_m_chars))]
return sum([abs(bin_n_ints[i] - bin_m_ints[i])
for i in range(len(bin_n_ints))])
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"
Feb. 7, 2019