Skip to content

Commit cb5723a

Browse files
author
oykusuoglu
committed
edit distance algorithm in tl package with docstring
1 parent 6477c43 commit cb5723a

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def edit_distance(string1: str, string2: str) -> int:
2+
"""Run the edit distance algorithm on string1 and string2.
3+
4+
Parameters
5+
----------
6+
string1
7+
first string
8+
string2
9+
second string
10+
11+
Returns
12+
-------
13+
Some integer value.
14+
"""
15+
if len(string1) == 0:
16+
ed = len(string2)
17+
return ed
18+
if len(string2) == 0:
19+
ed = len(string1)
20+
return ed
21+
elif string1[0] == string2[0]:
22+
return edit_distance(string1[1:], string2[1:])
23+
else:
24+
ed = 1 + min(
25+
edit_distance(string1[1:], string2),
26+
edit_distance(string1, string2[1:]),
27+
edit_distance(string1[1:], string2[1:]),
28+
)
29+
return ed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import myeditdistance as calc
2+
3+
print(calc.edit_distance("kitten", "sitting"))

0 commit comments

Comments
 (0)