Last active
August 29, 2015 14:09
-
-
Save alexandre/ed4ce97314f74bec7f53 to your computer and use it in GitHub Desktop.
learning about hamming distance - https://en.wikipedia.org/wiki/Hamming_distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| str_1 = 'this is a test' | |
| str_2 = 'wokka wokka!!!' | |
| dist = 0 | |
| for x,y in zip(str_1, str_2): | |
| val = ord(x) ^ ord(y) | |
| while val != 0: | |
| dist += 1 | |
| val &= val -1 | |
| dist == 37 # True | |
| # Bool is a subclass of int, so True == 1 and False == 0 | |
| sum(x != y for x,y in zip(str_1,str_2)) == 14 # True | |
| # ??? | |
| # some data to understand the process | |
| >>> pprint([(bin(ord(x)),bin(ord(y))) for x,y in zip(str_1,str_2)]) | |
| [('0b1110100', '0b1110111'), | |
| ('0b1101000', '0b1101111'), | |
| ('0b1101001', '0b1101011'), | |
| ('0b1110011', '0b1101011'), | |
| ('0b100000', '0b1100001'), | |
| ('0b1101001', '0b100000'), | |
| ('0b1110011', '0b1110111'), | |
| ('0b100000', '0b1101111'), | |
| ('0b1100001', '0b1101011'), | |
| ('0b100000', '0b1101011'), | |
| ('0b1110100', '0b1100001'), | |
| ('0b1100101', '0b100001'), | |
| ('0b1110011', '0b100001'), | |
| ('0b1110100', '0b100001')] | |
| # the diff between each tuple's elements: | |
| >>> pprint([(ord(x) ^ ord(y)) for x,y in zip(str_1,str_2)]) | |
| [3, 7, 2, 24, 65, 73, 4, 79, 10, 75, 21, 68, 82, 85] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment