forked from Crafter76/algo_distance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main1.py
41 lines (34 loc) · 1.08 KB
/
main1.py
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
35
36
37
38
39
40
41
import random
import string
import time
def edit_distance(s1, s2):
m, n = len(s1), len(s2)
if m < n:
return edit_distance(s2, s1)
previous = list(range(n+1))
for i, char1 in enumerate(s1, 1):
current = [i]
for j, char2 in enumerate(s2, 1):
current.append(min(current[j-1] + 1,
previous[j] + 1,
previous[j-1] + (char1 != char2)))
previous = current
return previous[n]
def main():
s1 = input()
s2 = input()
print(edit_distance(s1, s2))
def test(n_iter = 100):
for i in range(n_iter):
length = random.randint(0, 64)
s = ''.join(random.choice(string.ascii_letters) for _ in range(length))
assert edit_distance(s, '') == edit_distance('', s) == len(s)
assert edit_distance(s, s) == 0
assert edit_distance('distance', 'editing') == 5
assert edit_distance('hello', 'mello') == 1
if __name__ == '__main__':
for i in range(10):
start = time.time()
test(1000)
end = time.time()
print(end-start)