mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -19,37 +19,41 @@ class EditDistance:
|
||||
def __init__(self):
|
||||
self.__prepare__()
|
||||
|
||||
def __prepare__(self, N = 0, M = 0):
|
||||
self.dp = [[-1 for y in range(0,M)] for x in range(0,N)]
|
||||
def __prepare__(self, N=0, M=0):
|
||||
self.dp = [[-1 for y in range(0, M)] for x in range(0, N)]
|
||||
|
||||
def __solveDP(self, x, y):
|
||||
if (x==-1):
|
||||
return y+1
|
||||
elif (y==-1):
|
||||
return x+1
|
||||
elif (self.dp[x][y]>-1):
|
||||
if x == -1:
|
||||
return y + 1
|
||||
elif y == -1:
|
||||
return x + 1
|
||||
elif self.dp[x][y] > -1:
|
||||
return self.dp[x][y]
|
||||
else:
|
||||
if (self.A[x]==self.B[y]):
|
||||
self.dp[x][y] = self.__solveDP(x-1,y-1)
|
||||
if self.A[x] == self.B[y]:
|
||||
self.dp[x][y] = self.__solveDP(x - 1, y - 1)
|
||||
else:
|
||||
self.dp[x][y] = 1+min(self.__solveDP(x,y-1), self.__solveDP(x-1,y), self.__solveDP(x-1,y-1))
|
||||
self.dp[x][y] = 1 + min(
|
||||
self.__solveDP(x, y - 1),
|
||||
self.__solveDP(x - 1, y),
|
||||
self.__solveDP(x - 1, y - 1),
|
||||
)
|
||||
|
||||
return self.dp[x][y]
|
||||
|
||||
def solve(self, A, B):
|
||||
if isinstance(A,bytes):
|
||||
A = A.decode('ascii')
|
||||
if isinstance(A, bytes):
|
||||
A = A.decode("ascii")
|
||||
|
||||
if isinstance(B,bytes):
|
||||
B = B.decode('ascii')
|
||||
if isinstance(B, bytes):
|
||||
B = B.decode("ascii")
|
||||
|
||||
self.A = str(A)
|
||||
self.B = str(B)
|
||||
|
||||
self.__prepare__(len(A), len(B))
|
||||
|
||||
return self.__solveDP(len(A)-1, len(B)-1)
|
||||
return self.__solveDP(len(A) - 1, len(B) - 1)
|
||||
|
||||
|
||||
def min_distance_bottom_up(word1: str, word2: str) -> int:
|
||||
@ -63,38 +67,37 @@ def min_distance_bottom_up(word1: str, word2: str) -> int:
|
||||
"""
|
||||
m = len(word1)
|
||||
n = len(word2)
|
||||
dp = [[0 for _ in range(n+1) ] for _ in range(m+1)]
|
||||
for i in range(m+1):
|
||||
for j in range(n+1):
|
||||
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||
for i in range(m + 1):
|
||||
for j in range(n + 1):
|
||||
|
||||
if i == 0: #first string is empty
|
||||
if i == 0: # first string is empty
|
||||
dp[i][j] = j
|
||||
elif j == 0: #second string is empty
|
||||
elif j == 0: # second string is empty
|
||||
dp[i][j] = i
|
||||
elif word1[i-1] == word2[j-1]: #last character of both substing is equal
|
||||
dp[i][j] = dp[i-1][j-1]
|
||||
elif (
|
||||
word1[i - 1] == word2[j - 1]
|
||||
): # last character of both substing is equal
|
||||
dp[i][j] = dp[i - 1][j - 1]
|
||||
else:
|
||||
insert = dp[i][j-1]
|
||||
delete = dp[i-1][j]
|
||||
replace = dp[i-1][j-1]
|
||||
insert = dp[i][j - 1]
|
||||
delete = dp[i - 1][j]
|
||||
replace = dp[i - 1][j - 1]
|
||||
dp[i][j] = 1 + min(insert, delete, replace)
|
||||
return dp[m][n]
|
||||
|
||||
if __name__ == '__main__':
|
||||
solver = EditDistance()
|
||||
|
||||
print("****************** Testing Edit Distance DP Algorithm ******************")
|
||||
print()
|
||||
|
||||
S1 = input("Enter the first string: ").strip()
|
||||
S2 = input("Enter the second string: ").strip()
|
||||
|
||||
print()
|
||||
print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2)))
|
||||
print("The minimum Edit Distance is: %d" % (min_distance_bottom_up(S1, S2)))
|
||||
print()
|
||||
print("*************** End of Testing Edit Distance DP Algorithm ***************")
|
||||
|
||||
if __name__ == "__main__":
|
||||
solver = EditDistance()
|
||||
|
||||
print("****************** Testing Edit Distance DP Algorithm ******************")
|
||||
print()
|
||||
|
||||
S1 = input("Enter the first string: ").strip()
|
||||
S2 = input("Enter the second string: ").strip()
|
||||
|
||||
print()
|
||||
print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2)))
|
||||
print("The minimum Edit Distance is: %d" % (min_distance_bottom_up(S1, S2)))
|
||||
print()
|
||||
print("*************** End of Testing Edit Distance DP Algorithm ***************")
|
||||
|
Reference in New Issue
Block a user