forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_distances.py
More file actions
34 lines (26 loc) · 808 Bytes
/
string_distances.py
File metadata and controls
34 lines (26 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# https://en.wikipedia.org/wiki/Levenshtein_distance
import time
import random
def levenstein_distance(a, b):
m = len(a)
n = len(b)
dp = [[0] * (n + 1) for i in range(m + 1)]
for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j
for i in range(m):
for j in range(n):
if a[i] == b[j]:
dp[i + 1][j + 1] = dp[i][j]
else:
dp[i + 1][j + 1] = 1 + min(dp[i][j], dp[i + 1][j], dp[i][j + 1])
return dp[m][n]
def test():
start = time.time()
print(levenstein_distance('abc', 'ab'))
print(levenstein_distance(random_string(1000), random_string(1000)))
print(time.time() - start)
def random_string(len):
return ''.join(random.choice('ab') for _ in range(len))
test()