Skip to content

Commit 6c10551

Browse files
authored
fix typo
1 parent 46367d7 commit 6c10551

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

edit distance dynamic programming.py

+26-44
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77

88
#its also called levenshtein distance
99
#it can be done via recursion too
10+
#the recursion version is much more elegant yet less efficient
1011
# https://github.com/je-suis-tm/recursion/blob/master/edit%20distance%20recursion.py
1112

12-
#edit distance is to minimumize the steps transfering one string to another
13-
#the way to solve this problem, is very similar is to knapsack
13+
#edit distance is to minimize the steps transforming one string to another
14+
#the way to solve this problem is very similar is to knapsack
1415
#assume we have two strings a and b
1516
#we build a matrix len(a)*len(b)
16-
#however, given lists start at index zero
17+
#however,given lists start at index zero
1718
#our matrix should be (len(a)+1)*(len(b)+1)
1819

1920
#there are three different ways to transform a string
20-
#insert, delete and replace
21+
#insert,delete and replace
2122
#we can use any of them or combined
2223
#lets take a look at the best case first
2324
#assume string a is string b
@@ -27,81 +28,62 @@
2728
#when string a has nothing in common with string b
2829
#we would have to replace the whole string a
2930
#the steps become the maximum step which is max(len(a),len(b))
30-
#for general case, the steps would fall between the worst and the best case
31+
#for general case,the number of steps would fall between the worst and the best case
3132
#assume we are at mth letter of string a and nth letter string b
32-
#if we wanna get the optimized steps of transforming string a to string b
33+
#if we wanna get the optimal steps of transforming string a to string b
3334
#we have to make sure at each letter transformation
34-
#a[:m] and b[:n] have reached their optimization
35-
#otherwise, we could always find another combinations of insert, delete and replace
36-
#to get a more optimized a[:m] and b[:n]
37-
#it would make our string transformation not so optimized any more
35+
#a[:m] and b[:n] have reached their optimal status
36+
#otherwise,we could always find another combination of insert,delete and replace
37+
#to get a "real" optimal a[:m] and b[:n]
38+
#it would make our string transformation not so optimal any more
3839
#it is the same logic as the optimization of knapsack problem
3940
#after we set our logic straight
4041
#we would take a look at three different approaches
4142
#lets take a look at insertion
4243
#basically we need to insert nth letter from string b into string a at nth position
4344
#the cumulated steps we have taken should be matrix[m][n-1]+1
4445
#matrix[m][n-1] is the steps for a[:m] to b[:n]
45-
#for delete, it is vice versa
46+
#for delete,it is vice versa
4647
#the cumulated steps we have taken should be matrix[m-1][n]+1
47-
#for replacement, it is a lil bit tricky
48+
#for replacement,it is a lil bit tricky
4849
#there are two scenarios
4950
#if a[m]==b[n]
5051
#it should be matrix[m-1][n-1]
5152
#we dont need any replacement at all
52-
#else, it should be matrix[m-1][n-1]+1
53+
#else,it should be matrix[m-1][n-1]+1
5354
#we replace mth letter of string a with nth letter of string b
5455
#after we managed to understand three different approaches
55-
#we want to take the minimum steps among these three approaches
56+
#we want to take the minimum number of steps among these three approaches
5657
#throughout the iteration of different positions of both strings
57-
#in the end, we would get the optimized steps to transform one string to another, YAY
58-
def edit(a,b):
58+
#in the end,we would get the optimal steps to transform one string to another,YAY
59+
def edit_distance(a,b):
60+
5961
len_a=len(a)
6062
len_b=len(b)
6163

6264
#this part is to create a matrix of len(a)*len(b)
6365
#as lists start at index 0
6466
#we get a matrix of (len(a)+1)*(len(b)+1) instead
65-
c=[]
66-
for i in range(len_a+1):
67-
c.append([0]*(len_b+1))
67+
c=[[0]*(len_b+1) for i in range(len_a+1)]
68+
6869
for j in range(len_a+1):
6970
c[j][0]=j
7071
for k in range(len_b+1):
7172
c[0][k]=k
7273

7374
#we take iterations on both string a and b
74-
#next, we check if a[m]==b[n]
75-
#if yes, no replacement needed
76-
#if no, replacement needed
77-
#we take a minimum functions to see which combinations would give the minimum steps
75+
#next,we check if a[m]==b[n]
76+
#if yes,no replacement needed
77+
#if no,replacement needed
78+
#we take a minimum function to see which combination would give the minimum steps
7879
#eventually we got what we are after
7980
for l in range(1,len_a+1):
80-
for m in range(len_b+1):
81+
for m in range(1,len_b+1):
8182
if a[l-1]==b[m-1]:
8283
c[l][m]=min(c[l-1][m]+1,c[l][m-1]+1,c[l-1][m-1])
8384
else:
8485
c[l][m]=min(c[l-1][m]+1,c[l][m-1]+1,c[l-1][m-1]+1)
8586

8687
return c[len_a][len_b]
8788

88-
#lets get some random strings to test
89-
import random as rd
90-
91-
temp=rd.randint(1,20)
92-
temp1=rd.randint(1,20)
93-
alphabet='abcdefghijklmnopqrstuvwxyz'
94-
temp2=''
95-
temp3=''
96-
97-
for n in range(temp):
98-
temp2+=alphabet[rd.randint(0,25)]
99-
100-
for o in range(temp1):
101-
temp3+=alphabet[rd.randint(0,25)]
102-
103-
104-
105-
106-
print(temp2,temp3)
107-
print(edit(temp2,temp3))
89+
print(edit_distance('baiseé','bas'))

0 commit comments

Comments
 (0)