mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 06:10:19 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -6,49 +6,49 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
def edit_distance_dfs(s: str, t: str, i: int, j: int) -> int:
|
||||
"""Edit distance: Brute force search"""
|
||||
"""Edit distance: Brute-force search"""
|
||||
# If both s and t are empty, return 0
|
||||
if i == 0 and j == 0:
|
||||
return 0
|
||||
# If s is empty, return the length of t
|
||||
# If s is empty, return length of t
|
||||
if i == 0:
|
||||
return j
|
||||
# If t is empty, return the length of s
|
||||
# If t is empty, return length of s
|
||||
if j == 0:
|
||||
return i
|
||||
# If the two characters are equal, skip these two characters
|
||||
# If two characters are equal, skip both characters
|
||||
if s[i - 1] == t[j - 1]:
|
||||
return edit_distance_dfs(s, t, i - 1, j - 1)
|
||||
# The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
insert = edit_distance_dfs(s, t, i, j - 1)
|
||||
delete = edit_distance_dfs(s, t, i - 1, j)
|
||||
replace = edit_distance_dfs(s, t, i - 1, j - 1)
|
||||
# Return the minimum number of edits
|
||||
# Return minimum edit steps
|
||||
return min(insert, delete, replace) + 1
|
||||
|
||||
|
||||
def edit_distance_dfs_mem(s: str, t: str, mem: list[list[int]], i: int, j: int) -> int:
|
||||
"""Edit distance: Memoized search"""
|
||||
"""Edit distance: Memoization search"""
|
||||
# If both s and t are empty, return 0
|
||||
if i == 0 and j == 0:
|
||||
return 0
|
||||
# If s is empty, return the length of t
|
||||
# If s is empty, return length of t
|
||||
if i == 0:
|
||||
return j
|
||||
# If t is empty, return the length of s
|
||||
# If t is empty, return length of s
|
||||
if j == 0:
|
||||
return i
|
||||
# If there is a record, return it
|
||||
# If there's a record, return it directly
|
||||
if mem[i][j] != -1:
|
||||
return mem[i][j]
|
||||
# If the two characters are equal, skip these two characters
|
||||
# If two characters are equal, skip both characters
|
||||
if s[i - 1] == t[j - 1]:
|
||||
return edit_distance_dfs_mem(s, t, mem, i - 1, j - 1)
|
||||
# The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
insert = edit_distance_dfs_mem(s, t, mem, i, j - 1)
|
||||
delete = edit_distance_dfs_mem(s, t, mem, i - 1, j)
|
||||
replace = edit_distance_dfs_mem(s, t, mem, i - 1, j - 1)
|
||||
# Record and return the minimum number of edits
|
||||
# Record and return minimum edit steps
|
||||
mem[i][j] = min(insert, delete, replace) + 1
|
||||
return mem[i][j]
|
||||
|
||||
@@ -62,14 +62,14 @@ def edit_distance_dp(s: str, t: str) -> int:
|
||||
dp[i][0] = i
|
||||
for j in range(1, m + 1):
|
||||
dp[0][j] = j
|
||||
# State transition: the rest of the rows and columns
|
||||
# State transition: rest of the rows and columns
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
if s[i - 1] == t[j - 1]:
|
||||
# If the two characters are equal, skip these two characters
|
||||
# If two characters are equal, skip both characters
|
||||
dp[i][j] = dp[i - 1][j - 1]
|
||||
else:
|
||||
# The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
|
||||
return dp[n][m]
|
||||
|
||||
@@ -81,21 +81,21 @@ def edit_distance_dp_comp(s: str, t: str) -> int:
|
||||
# State transition: first row
|
||||
for j in range(1, m + 1):
|
||||
dp[j] = j
|
||||
# State transition: the rest of the rows
|
||||
# State transition: rest of the rows
|
||||
for i in range(1, n + 1):
|
||||
# State transition: first column
|
||||
leftup = dp[0] # Temporarily store dp[i-1, j-1]
|
||||
dp[0] += 1
|
||||
# State transition: the rest of the columns
|
||||
# State transition: rest of the columns
|
||||
for j in range(1, m + 1):
|
||||
temp = dp[j]
|
||||
if s[i - 1] == t[j - 1]:
|
||||
# If the two characters are equal, skip these two characters
|
||||
# If two characters are equal, skip both characters
|
||||
dp[j] = leftup
|
||||
else:
|
||||
# The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
dp[j] = min(dp[j - 1], dp[j], leftup) + 1
|
||||
leftup = temp # Update for the next round of dp[i-1, j-1]
|
||||
leftup = temp # Update for next round's dp[i-1, j-1]
|
||||
return dp[m]
|
||||
|
||||
|
||||
@@ -105,19 +105,19 @@ if __name__ == "__main__":
|
||||
t = "pack"
|
||||
n, m = len(s), len(t)
|
||||
|
||||
# Brute force search
|
||||
# Brute-force search
|
||||
res = edit_distance_dfs(s, t, n, m)
|
||||
print(f"To change {s} to {t}, the minimum number of edits required is {res}")
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
|
||||
# Memoized search
|
||||
# Memoization search
|
||||
mem = [[-1] * (m + 1) for _ in range(n + 1)]
|
||||
res = edit_distance_dfs_mem(s, t, mem, n, m)
|
||||
print(f"To change {s} to {t}, the minimum number of edits required is {res}")
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
|
||||
# Dynamic programming
|
||||
res = edit_distance_dp(s, t)
|
||||
print(f"To change {s} to {t}, the minimum number of edits required is {res}")
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = edit_distance_dp_comp(s, t)
|
||||
print(f"To change {s} to {t}, the minimum number of edits required is {res}")
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
|
||||
Reference in New Issue
Block a user