Skip to content

Commit 39e5bc5

Browse files
tianyizheng02github-actionsChrisO345
authored
Refactor bottom-up edit distance function to be class method (TheAlgorithms#7347)
* Refactor bottom-up function to be class method * Add type hints * Update convolve function namespace * Remove depreciated np.float * updating DIRECTORY.md * updating DIRECTORY.md * updating DIRECTORY.md * updating DIRECTORY.md * Renamed function for consistency * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Chris O <[email protected]>
1 parent f8958eb commit 39e5bc5

File tree

2 files changed

+80
-69
lines changed

2 files changed

+80
-69
lines changed

DIRECTORY.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* [Count Number Of One Bits](bit_manipulation/count_number_of_one_bits.py)
4747
* [Gray Code Sequence](bit_manipulation/gray_code_sequence.py)
4848
* [Highest Set Bit](bit_manipulation/highest_set_bit.py)
49+
* [Index Of Rightmost Set Bit](bit_manipulation/index_of_rightmost_set_bit.py)
4950
* [Is Even](bit_manipulation/is_even.py)
5051
* [Reverse Bits](bit_manipulation/reverse_bits.py)
5152
* [Single Bit Manipulation Operations](bit_manipulation/single_bit_manipulation_operations.py)
@@ -307,24 +308,28 @@
307308
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
308309
* [Max Sub Array](dynamic_programming/max_sub_array.py)
309310
* [Max Sum Contiguous Subsequence](dynamic_programming/max_sum_contiguous_subsequence.py)
311+
* [Min Distance Up Bottom](dynamic_programming/min_distance_up_bottom.py)
310312
* [Minimum Coin Change](dynamic_programming/minimum_coin_change.py)
311313
* [Minimum Cost Path](dynamic_programming/minimum_cost_path.py)
312314
* [Minimum Partition](dynamic_programming/minimum_partition.py)
313315
* [Minimum Squares To Represent A Number](dynamic_programming/minimum_squares_to_represent_a_number.py)
314316
* [Minimum Steps To One](dynamic_programming/minimum_steps_to_one.py)
315317
* [Optimal Binary Search Tree](dynamic_programming/optimal_binary_search_tree.py)
318+
* [Palindrome Partitioning](dynamic_programming/palindrome_partitioning.py)
316319
* [Rod Cutting](dynamic_programming/rod_cutting.py)
317320
* [Subset Generation](dynamic_programming/subset_generation.py)
318321
* [Sum Of Subset](dynamic_programming/sum_of_subset.py)
319322
* [Viterbi](dynamic_programming/viterbi.py)
320323

321324
## Electronics
325+
* [Builtin Voltage](electronics/builtin_voltage.py)
322326
* [Carrier Concentration](electronics/carrier_concentration.py)
323327
* [Coulombs Law](electronics/coulombs_law.py)
324328
* [Electric Conductivity](electronics/electric_conductivity.py)
325329
* [Electric Power](electronics/electric_power.py)
326330
* [Electrical Impedance](electronics/electrical_impedance.py)
327331
* [Ohms Law](electronics/ohms_law.py)
332+
* [Resistor Equivalence](electronics/resistor_equivalence.py)
328333
* [Resonant Frequency](electronics/resonant_frequency.py)
329334

330335
## File Transfer
@@ -426,6 +431,7 @@
426431
* [Adler32](hashes/adler32.py)
427432
* [Chaos Machine](hashes/chaos_machine.py)
428433
* [Djb2](hashes/djb2.py)
434+
* [Elf](hashes/elf.py)
429435
* [Enigma Machine](hashes/enigma_machine.py)
430436
* [Hamming Code](hashes/hamming_code.py)
431437
* [Luhn](hashes/luhn.py)
@@ -491,6 +497,7 @@
491497
* [Abs Max](maths/abs_max.py)
492498
* [Abs Min](maths/abs_min.py)
493499
* [Add](maths/add.py)
500+
* [Addition Without Arithmetic](maths/addition_without_arithmetic.py)
494501
* [Aliquot Sum](maths/aliquot_sum.py)
495502
* [Allocation Number](maths/allocation_number.py)
496503
* [Arc Length](maths/arc_length.py)
@@ -581,12 +588,15 @@
581588
* [Points Are Collinear 3D](maths/points_are_collinear_3d.py)
582589
* [Pollard Rho](maths/pollard_rho.py)
583590
* [Polynomial Evaluation](maths/polynomial_evaluation.py)
591+
* Polynomials
592+
* [Single Indeterminate Operations](maths/polynomials/single_indeterminate_operations.py)
584593
* [Power Using Recursion](maths/power_using_recursion.py)
585594
* [Prime Check](maths/prime_check.py)
586595
* [Prime Factors](maths/prime_factors.py)
587596
* [Prime Numbers](maths/prime_numbers.py)
588597
* [Prime Sieve Eratosthenes](maths/prime_sieve_eratosthenes.py)
589598
* [Primelib](maths/primelib.py)
599+
* [Print Multiplication Table](maths/print_multiplication_table.py)
590600
* [Proth Number](maths/proth_number.py)
591601
* [Pythagoras](maths/pythagoras.py)
592602
* [Qr Decomposition](maths/qr_decomposition.py)
@@ -676,12 +686,15 @@
676686
* [Magicdiamondpattern](other/magicdiamondpattern.py)
677687
* [Maximum Subarray](other/maximum_subarray.py)
678688
* [Nested Brackets](other/nested_brackets.py)
689+
* [Pascal Triangle](other/pascal_triangle.py)
679690
* [Password Generator](other/password_generator.py)
691+
* [Quine](other/quine.py)
680692
* [Scoring Algorithm](other/scoring_algorithm.py)
681693
* [Sdes](other/sdes.py)
682694
* [Tower Of Hanoi](other/tower_of_hanoi.py)
683695

684696
## Physics
697+
* [Archimedes Principle](physics/archimedes_principle.py)
685698
* [Casimir Effect](physics/casimir_effect.py)
686699
* [Centripetal Force](physics/centripetal_force.py)
687700
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
@@ -694,7 +707,7 @@
694707
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
695708
* [Potential Energy](physics/potential_energy.py)
696709
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
697-
* [Sheer Stress](physics/sheer_stress.py)
710+
* [Shear Stress](physics/shear_stress.py)
698711

699712
## Project Euler
700713
* Problem 001

dynamic_programming/edit_distance.py

+66-68
Original file line numberDiff line numberDiff line change
@@ -19,74 +19,72 @@ class EditDistance:
1919
"""
2020

2121
def __init__(self):
22-
self.__prepare__()
23-
24-
def __prepare__(self, n=0, m=0):
25-
self.dp = [[-1 for y in range(0, m)] for x in range(0, n)]
26-
27-
def __solve_dp(self, x, y):
28-
if x == -1:
29-
return y + 1
30-
elif y == -1:
31-
return x + 1
32-
elif self.dp[x][y] > -1:
33-
return self.dp[x][y]
22+
self.word1 = ""
23+
self.word2 = ""
24+
self.dp = []
25+
26+
def __min_dist_top_down_dp(self, m: int, n: int) -> int:
27+
if m == -1:
28+
return n + 1
29+
elif n == -1:
30+
return m + 1
31+
elif self.dp[m][n] > -1:
32+
return self.dp[m][n]
3433
else:
35-
if self.a[x] == self.b[y]:
36-
self.dp[x][y] = self.__solve_dp(x - 1, y - 1)
34+
if self.word1[m] == self.word2[n]:
35+
self.dp[m][n] = self.__min_dist_top_down_dp(m - 1, n - 1)
3736
else:
38-
self.dp[x][y] = 1 + min(
39-
self.__solve_dp(x, y - 1),
40-
self.__solve_dp(x - 1, y),
41-
self.__solve_dp(x - 1, y - 1),
42-
)
43-
44-
return self.dp[x][y]
45-
46-
def solve(self, a, b):
47-
if isinstance(a, bytes):
48-
a = a.decode("ascii")
49-
50-
if isinstance(b, bytes):
51-
b = b.decode("ascii")
52-
53-
self.a = str(a)
54-
self.b = str(b)
55-
56-
self.__prepare__(len(a), len(b))
57-
58-
return self.__solve_dp(len(a) - 1, len(b) - 1)
59-
60-
61-
def min_distance_bottom_up(word1: str, word2: str) -> int:
62-
"""
63-
>>> min_distance_bottom_up("intention", "execution")
64-
5
65-
>>> min_distance_bottom_up("intention", "")
66-
9
67-
>>> min_distance_bottom_up("", "")
68-
0
69-
"""
70-
m = len(word1)
71-
n = len(word2)
72-
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
73-
for i in range(m + 1):
74-
for j in range(n + 1):
75-
76-
if i == 0: # first string is empty
77-
dp[i][j] = j
78-
elif j == 0: # second string is empty
79-
dp[i][j] = i
80-
elif (
81-
word1[i - 1] == word2[j - 1]
82-
): # last character of both substing is equal
83-
dp[i][j] = dp[i - 1][j - 1]
84-
else:
85-
insert = dp[i][j - 1]
86-
delete = dp[i - 1][j]
87-
replace = dp[i - 1][j - 1]
88-
dp[i][j] = 1 + min(insert, delete, replace)
89-
return dp[m][n]
37+
insert = self.__min_dist_top_down_dp(m, n - 1)
38+
delete = self.__min_dist_top_down_dp(m - 1, n)
39+
replace = self.__min_dist_top_down_dp(m - 1, n - 1)
40+
self.dp[m][n] = 1 + min(insert, delete, replace)
41+
42+
return self.dp[m][n]
43+
44+
def min_dist_top_down(self, word1: str, word2: str) -> int:
45+
"""
46+
>>> EditDistance().min_dist_top_down("intention", "execution")
47+
5
48+
>>> EditDistance().min_dist_top_down("intention", "")
49+
9
50+
>>> EditDistance().min_dist_top_down("", "")
51+
0
52+
"""
53+
self.word1 = word1
54+
self.word2 = word2
55+
self.dp = [[-1 for _ in range(len(word2))] for _ in range(len(word1))]
56+
57+
return self.__min_dist_top_down_dp(len(word1) - 1, len(word2) - 1)
58+
59+
def min_dist_bottom_up(self, word1: str, word2: str) -> int:
60+
"""
61+
>>> EditDistance().min_dist_bottom_up("intention", "execution")
62+
5
63+
>>> EditDistance().min_dist_bottom_up("intention", "")
64+
9
65+
>>> EditDistance().min_dist_bottom_up("", "")
66+
0
67+
"""
68+
self.word1 = word1
69+
self.word2 = word2
70+
m = len(word1)
71+
n = len(word2)
72+
self.dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
73+
74+
for i in range(m + 1):
75+
for j in range(n + 1):
76+
if i == 0: # first string is empty
77+
self.dp[i][j] = j
78+
elif j == 0: # second string is empty
79+
self.dp[i][j] = i
80+
elif word1[i - 1] == word2[j - 1]: # last characters are equal
81+
self.dp[i][j] = self.dp[i - 1][j - 1]
82+
else:
83+
insert = self.dp[i][j - 1]
84+
delete = self.dp[i - 1][j]
85+
replace = self.dp[i - 1][j - 1]
86+
self.dp[i][j] = 1 + min(insert, delete, replace)
87+
return self.dp[m][n]
9088

9189

9290
if __name__ == "__main__":
@@ -99,7 +97,7 @@ def min_distance_bottom_up(word1: str, word2: str) -> int:
9997
S2 = input("Enter the second string: ").strip()
10098

10199
print()
102-
print(f"The minimum Edit Distance is: {solver.solve(S1, S2)}")
103-
print(f"The minimum Edit Distance is: {min_distance_bottom_up(S1, S2)}")
100+
print(f"The minimum edit distance is: {solver.min_dist_top_down(S1, S2)}")
101+
print(f"The minimum edit distance is: {solver.min_dist_bottom_up(S1, S2)}")
104102
print()
105103
print("*************** End of Testing Edit Distance DP Algorithm ***************")

0 commit comments

Comments
 (0)