Skip to content

Instantly share code, notes, and snippets.

@gsathya
Last active January 4, 2016 13:09
Show Gist options
  • Save gsathya/8626549 to your computer and use it in GitHub Desktop.
Save gsathya/8626549 to your computer and use it in GitHub Desktop.
def edit_distance(word1, word2):
len1 = len(word1)
len2 = len(word2)
table = [[0]*(len2+1) for i in range(len1+1)]
table2 = [[0]*(len2+1)]*(len1+1)
print table, "\n", table2
for i in range(len1+1):
table[i][0] = i
for j in range(len2+1):
table[0][j] = j
for i in range(1, len1+1):
for j in range(1, len2+1):
diag = 0 if word1[i-1] == word2[j-1] else 1
table[i][j] = min([table[i-1][j-1]+diag,
table[i-1][j]+1,
table[i][j-1]+1])
print table
return table[len1][len2]
print edit_distance("cat", "catt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment