Last active
January 29, 2016 19:57
-
-
Save nohe427/dd17b72f8aac62d6682f to your computer and use it in GitHub Desktop.
A quick proof of concept for calculating the hamming distance between words to find the shortest distance
This file contains 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
def calculateHammingDistance(): | |
closestWord = None | |
prevWordDistance = None | |
TestWord = "work" | |
DictWords = ["four", "That", "Thin", "Test", "This", "NINJA"] | |
for word in DictWords: | |
if (len(TestWord) == len(word)): | |
wordDist = distance(TestWord, word) | |
if (prevWordDistance == None or prevWordDistance > wordDist): | |
prevWordDistance = wordDist | |
closestWord = word | |
print closestWord | |
def distance(yourWord, dicitonaryWord): | |
i = 0 | |
letterDistance = 0 | |
for letter in yourWord: | |
binaryLetterYourWord = bin(ord(letter)) | |
binLetterDictWord = bin(ord(dicitonaryWord[i])) | |
x = 0 | |
for binLet in binaryLetterYourWord: | |
if (binLet != binLetterDictWord[x]): | |
letterDistance = letterDistance + 1 | |
x=x+1 | |
i = i+1 | |
print letterDistance | |
return letterDistance | |
if __name__ == "__main__": | |
calculateHammingDistance() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment