forked from aayn/it2-python-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitws.py
22 lines (20 loc) · 770 Bytes
/
itws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from functools import reduce, partial
from operator import add
def compose(*fns):
compose_binary = lambda f, g: lambda x: f(g(x))
return reduce(compose_binary, fns, lambda x: x)
def edit_distance(s1,s2):
if len(s1) > len(s2):
s1,s2 = s2,s1
distances = range(len(s1) + 1)
for index2,char2 in enumerate(s2):
newDistances = [index2+1]
for index1,char1 in enumerate(s1):
if char1 == char2:
newDistances.append(distances[index1])
else:
newDistances.append(1 + min((distances[index1],
distances[index1+1],
newDistances[-1])))
distances = newDistances
return distances[-1]