forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ZoranPandovski#417 from noblehelm/add_levenshtein_…
…in_python add levenshtein distance in python
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
def levenshtein(first, second): | ||
n1 = len(first) | ||
n2 = len(second) | ||
t = [0] * (n2 + 1) | ||
for i in range(0,n2): | ||
t[i] = i | ||
for i in range(0,n1): | ||
t[0] = i + 1 | ||
corner = i | ||
for j in range(0,n2): | ||
upper = t[j + 1] | ||
if (first[i] == second[j]): | ||
t[j + 1] = corner | ||
else: | ||
t[j + 1] = min(t[j], min(upper,corner)) + 1 | ||
corner = upper | ||
return t[n2] | ||
|
||
def test(): | ||
print(levenshtein('levenshtein','meilenstein')) | ||
print(levenshtein('github','bitbucket')) | ||
|
||
if __name__ == '__main__': | ||
test() |