Skip to content

Commit

Permalink
Merge d94adb1 into 3f9aa45
Browse files Browse the repository at this point in the history
  • Loading branch information
dunderbruno committed Sep 27, 2018
2 parents 3f9aa45 + d94adb1 commit 94c7ab4
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ If you want to uninstall algorithms, it is as simple as:
- [longest_common_prefix](algorithms/strings/longest_common_prefix.py)
- [rotate](algorithms/strings/rotate.py)
- [first_unique_char](algorithms/strings/first_unique_char.py)
- [repeat_substring](algorithms/strings/repeat_substring.py)
- [repeat_substring](algorithms/strings/repeat_substring.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree](algorithms/tree)
- [bst](algorithms/tree/bst)
- [array_to_bst](algorithms/tree/bst/array_to_bst.py)
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ pip3 uninstall -y algorithms
- [reverse_words:反转单词](algorithms/strings/reverse_words.py)
- [roman_to_int:罗马数转换整数](algorithms/strings/roman_to_int.py)
- [word_squares:单词平方](algorithms/strings/word_squares.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree:树](algorithms/tree)
- [segment-tree:线段树](algorithms/tree/segment_tree)
- [segment_tree:线段树](algorithms/tree/segment_tree/segment_tree.py)
Expand Down
3 changes: 2 additions & 1 deletion README_FR.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ Si vous voulez désinstaller le paquet algorithms, il suffit de procéder comme
- [longest_common_prefix](algorithms/strings/longest_common_prefix.py)
- [rotate](algorithms/strings/rotate.py)
- [first_unique_char](algorithms/strings/first_unique_char.py)
- [repeat_substring](algorithms/strings/repeat_substring.py)
- [repeat_substring](algorithms/strings/repeat_substring.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree](algorithms/tree)
- [bst](algorithms/tree/tree/bst)
- [array2bst](algorithms/tree/bst/array2bst.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [reverse_words](algorithms/strings/reverse_words.py)
- [roman_to_int](algorithms/strings/roman_to_int.py)
- [word_squares](algorithms/strings/word_squares.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree](algorithms/tree)
- [bst](algorithms/tree/tree/bst)
- [array2bst](algorithms/tree/bst/array2bst.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ if __name__ == "__main__":
- [reverse_words](algorithms/strings/reverse_words.py)
- [roman_to_int](algorithms/strings/roman_to_int.py)
- [word_squares](algorithms/strings/word_squares.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree : 木構造](algorithms/tree)
- [bst](algorithms/tree/tree/bst)
- [array2bst](algorithms/tree/bst/array2bst.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ if __name__ == "__main__":
- [reverse_words](algorithms/strings/reverse_words.py)
- [roman_to_int](algorithms/strings/roman_to_int.py)
- [word_squares](algorithms/strings/word_squares.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree : 트리](algorithms/tree)
- [bst : 이진 탐색 트리](algorithms/tree/tree/bst)
- [array2bst](algorithms/tree/bst/array2bst.py)
Expand Down
1 change: 1 addition & 0 deletions README_PTBR.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ Se você deseja desinstalar os algoritmos, é tão simples quanto:
- [caesar_cipher](algorithms/strings/caesar_cipher.py)
- [contain_string](algorithms/strings/contain_string.py)
- [count_binary_substring](algorithms/strings/count_binary_substring.py)
- [levenshtein distance](algorithms/strings/levenshtein.py)
- [tree](algorithms/tree)
- [bst](algorithms/tree/tree/bst)
- [array2bst](algorithms/tree/bst/array2bst.py)
Expand Down
54 changes: 54 additions & 0 deletions algorithms/strings/levenshtein.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Levenshtein Distance.
In information theory, linguistics and computer science,
the Levenshtein distance is a string metric for measuring the difference
between two sequences. Informally, the Levenshtein distance between two words
is the minimum number of single-character edits (insertions, deletions or
substitutions) required to change one word into the other.
It is named after the Soviet mathematician Vladimir Levenshtein, who considered
this distance in 1965.[1]
Levenshtein distance may also be referred to as edit distance, although that
term may also denote a larger family of distance metrics.[2]:32 It is closely
related to pairwise string alignments.
https://en.wikipedia.org/wiki/Levenshtein_distance (27/09/2018)
"""


def levenshtein(A, B):
"""
Return the edit distance between two strings.
Example:
>>> from levenshtein import levenshtein
>>> levenshtein("Star Wars", "Star Trek")
>>> 4
"""
m = len(A)
n = len(B)
M = []

for x in range(m+1):
M.append([])
for y in range(n+1):
M[x].append((0))

for i in range(0, m+1):
M[i][0] = i

for j in range(0, n+1):
M[0][j] = j

for x in range(1, m+1):
for y in range(1, n+1):
if A[x-1] == B[y-1]:
cost = 0
else:
cost = 1

M[x][y] = min((M[x-1][y-1] + cost,
M[x-1][y] + 1,
M[x][y-1] + 1,))

return(M[m][n])

0 comments on commit 94c7ab4

Please sign in to comment.