Skip to content

Instantly share code, notes, and snippets.

@nohe427
Last active January 29, 2016 19:57
Show Gist options
  • Save nohe427/dd17b72f8aac62d6682f to your computer and use it in GitHub Desktop.
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
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