-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHamming Distance.py
More file actions
31 lines (25 loc) · 826 Bytes
/
Copy pathHamming Distance.py
File metadata and controls
31 lines (25 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python
'''
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, return the Hamming distance between them.
'''
def main():
x = int(input("Enter number: "))
y = int(input("Enter number: "))
counter = 0
x = format(x, '08b')
y = format(y, '08b')
#Below code would be helpful incase the binary number has more than 8 bits
if len(x) != len(y):
if len(x) > len(y):
a = len(x) - len(y)
y = ("0" * a) + y
elif len(y) > len(x):
a = len(y) - len(x)
x = ("0" * a) + x
for n in range(len(x)):
if x[n] != y[n]:
counter += 1
print(counter)
if __name__ == "__main__":
main()