Merge pull request #426 from rohitbabugaddeti/patch-5
update hamming_distance.md
This commit is contained in:
@ -7,11 +7,11 @@ Calculates the Hamming distance between two values.
|
||||
|
||||
- Use the XOR operator (`^`) to find the bit difference between the two numbers.
|
||||
- Use `bin()` to convert the result to a binary string.
|
||||
- Convert the string to a list and use `list.count()` to count and return the number of `1`s in it.
|
||||
- Convert the string to a list and use `count()` of `str` class to count and return the number of `1`s in it.
|
||||
|
||||
```py
|
||||
def hamming_distance(a, b):
|
||||
return list(bin(a ^ b)).count('1')
|
||||
return bin(a ^ b).count('1')
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user