mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
43
ebook_src/builtin_structures/find_edit_distance.py
Executable file
43
ebook_src/builtin_structures/find_edit_distance.py
Executable file
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' computes the edit distance between two strings '''
|
||||
|
||||
|
||||
def find_edit_distance(str1, str2):
|
||||
'''
|
||||
>>> s = 'sunday'
|
||||
>>> t = 'saturday'
|
||||
>>> find_edit_distance(s, t)
|
||||
3
|
||||
'''
|
||||
|
||||
m = len(str1)
|
||||
n = len(str2)
|
||||
diff = lambda c1, c2: 0 if c1 == c2 else 1
|
||||
|
||||
E = [[0] * (n + 1) for i in range(m + 1)]
|
||||
|
||||
for i in range(m + 1):
|
||||
E[i][0] = i
|
||||
|
||||
for j in range(1, n + 1):
|
||||
E[0][j] = j
|
||||
|
||||
for i in range(1, m + 1):
|
||||
for j in range(1, n + 1):
|
||||
E[i][j] = min(E[i-1][j] + 1, E[i][j-1] + 1, E[i-1][j-1] + diff(str1[i-1], str2[j-1]))
|
||||
|
||||
return E[m][n]
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue