Skip to content

Commit

Permalink
update hamming_distance.md
Browse files Browse the repository at this point in the history
`list()` is not required since `str` itself has `count()`
  • Loading branch information
rohitbabugaddeti authored Feb 18, 2021
1 parent bfdb98c commit c7acc67
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions snippets/hamming_distance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c7acc67

Please sign in to comment.