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:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions

View File

@@ -14,35 +14,35 @@ def backtrack(
diags1: list[bool],
diags2: list[bool],
):
"""Backtracking algorithm: n queens"""
"""Backtracking algorithm: N queens"""
# When all rows are placed, record the solution
if row == n:
res.append([list(row) for row in state])
return
# Traverse all columns
for col in range(n):
# Calculate the main and minor diagonals corresponding to the cell
# Calculate the main diagonal and anti-diagonal corresponding to this cell
diag1 = row - col + n - 1
diag2 = row + col
# Pruning: do not allow queens on the column, main diagonal, or minor diagonal of the cell
# Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
if not cols[col] and not diags1[diag1] and not diags2[diag2]:
# Attempt: place the queen in the cell
# Attempt: place the queen in this cell
state[row][col] = "Q"
cols[col] = diags1[diag1] = diags2[diag2] = True
# Place the next row
backtrack(row + 1, n, state, res, cols, diags1, diags2)
# Retract: restore the cell to an empty spot
# Backtrack: restore this cell to an empty cell
state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = False
def n_queens(n: int) -> list[list[list[str]]]:
"""Solve n queens"""
# Initialize an n*n size chessboard, where 'Q' represents the queen and '#' represents an empty spot
"""Solve N queens"""
# Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
state = [["#" for _ in range(n)] for _ in range(n)]
cols = [False] * n # Record columns with queens
diags1 = [False] * (2 * n - 1) # Record main diagonals with queens
diags2 = [False] * (2 * n - 1) # Record minor diagonals with queens
cols = [False] * n # Record whether there is a queen in the column
diags1 = [False] * (2 * n - 1) # Record whether there is a queen on the main diagonal
diags2 = [False] * (2 * n - 1) # Record whether there is a queen on the anti-diagonal
res = []
backtrack(0, n, state, res, cols, diags1, diags2)
@@ -54,8 +54,8 @@ if __name__ == "__main__":
n = 4
res = n_queens(n)
print(f"Input chessboard dimensions as {n}")
print(f"The total number of queen placement solutions is {len(res)}")
print(f"Input chessboard size is {n}")
print(f"There are {len(res)} queen placement solutions")
for state in res:
print("--------------------")
for row in state:

View File

@@ -8,7 +8,7 @@ Author: krahets (krahets@163.com)
def backtrack(
state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]
):
"""Backtracking algorithm: Permutation I"""
"""Backtracking algorithm: Permutations I"""
# When the state length equals the number of elements, record the solution
if len(state) == len(choices):
res.append(list(state))
@@ -17,18 +17,18 @@ def backtrack(
for i, choice in enumerate(choices):
# Pruning: do not allow repeated selection of elements
if not selected[i]:
# Attempt: make a choice, update the state
# Attempt: make choice, update state
selected[i] = True
state.append(choice)
# Proceed to the next round of selection
backtrack(state, choices, selected, res)
# Retract: undo the choice, restore to the previous state
# Backtrack: undo choice, restore to previous state
selected[i] = False
state.pop()
def permutations_i(nums: list[int]) -> list[list[int]]:
"""Permutation I"""
"""Permutations I"""
res = []
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
return res

View File

@@ -8,7 +8,7 @@ Author: krahets (krahets@163.com)
def backtrack(
state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]
):
"""Backtracking algorithm: Permutation II"""
"""Backtracking algorithm: Permutations II"""
# When the state length equals the number of elements, record the solution
if len(state) == len(choices):
res.append(list(state))
@@ -18,19 +18,19 @@ def backtrack(
for i, choice in enumerate(choices):
# Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if not selected[i] and choice not in duplicated:
# Attempt: make a choice, update the state
duplicated.add(choice) # Record selected element values
# Attempt: make choice, update state
duplicated.add(choice) # Record the selected element value
selected[i] = True
state.append(choice)
# Proceed to the next round of selection
backtrack(state, choices, selected, res)
# Retract: undo the choice, restore to the previous state
# Backtrack: undo choice, restore to previous state
selected[i] = False
state.pop()
def permutations_ii(nums: list[int]) -> list[list[int]]:
"""Permutation II"""
"""Permutations II"""
res = []
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
return res

View File

@@ -12,7 +12,7 @@ from modules import TreeNode, print_tree, list_to_tree
def pre_order(root: TreeNode):
"""Pre-order traversal: Example one"""
"""Preorder traversal: Example 1"""
if root is None:
return
if root.val == 7:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
print("\nInitialize binary tree")
print_tree(root)
# Pre-order traversal
# Preorder traversal
res = list[TreeNode]()
pre_order(root)

View File

@@ -12,7 +12,7 @@ from modules import TreeNode, print_tree, list_to_tree
def pre_order(root: TreeNode):
"""Pre-order traversal: Example two"""
"""Preorder traversal: Example 2"""
if root is None:
return
# Attempt
@@ -22,7 +22,7 @@ def pre_order(root: TreeNode):
res.append(list(path))
pre_order(root.left)
pre_order(root.right)
# Retract
# Backtrack
path.pop()
@@ -32,11 +32,11 @@ if __name__ == "__main__":
print("\nInitialize binary tree")
print_tree(root)
# Pre-order traversal
# Preorder traversal
path = list[TreeNode]()
res = list[list[TreeNode]]()
pre_order(root)
print("\nOutput all root-to-node 7 paths")
print("\nOutput all paths from root node to node 7")
for path in res:
print([node.val for node in path])

View File

@@ -12,7 +12,7 @@ from modules import TreeNode, print_tree, list_to_tree
def pre_order(root: TreeNode):
"""Pre-order traversal: Example three"""
"""Preorder traversal: Example 3"""
# Pruning
if root is None or root.val == 3:
return
@@ -23,7 +23,7 @@ def pre_order(root: TreeNode):
res.append(list(path))
pre_order(root.left)
pre_order(root.right)
# Retract
# Backtrack
path.pop()
@@ -33,11 +33,11 @@ if __name__ == "__main__":
print("\nInitialize binary tree")
print_tree(root)
# Pre-order traversal
# Preorder traversal
path = list[TreeNode]()
res = list[list[TreeNode]]()
pre_order(root)
print("\nOutput all root-to-node 7 paths, not including nodes with value 3")
print("\nOutput all paths from root node to node 7, excluding paths with nodes of value 3")
for path in res:
print([node.val for node in path])

View File

@@ -12,7 +12,7 @@ from modules import TreeNode, print_tree, list_to_tree
def is_solution(state: list[TreeNode]) -> bool:
"""Determine if the current state is a solution"""
"""Check if the current state is a solution"""
return state and state[-1].val == 7
@@ -22,7 +22,7 @@ def record_solution(state: list[TreeNode], res: list[list[TreeNode]]):
def is_valid(state: list[TreeNode], choice: TreeNode) -> bool:
"""Determine if the choice is legal under the current state"""
"""Check if the choice is valid under the current state"""
return choice is not None and choice.val != 3
@@ -39,20 +39,20 @@ def undo_choice(state: list[TreeNode], choice: TreeNode):
def backtrack(
state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]
):
"""Backtracking algorithm: Example three"""
# Check if it's a solution
"""Backtracking algorithm: Example 3"""
# Check if it is a solution
if is_solution(state):
# Record solution
record_solution(state, res)
# Traverse all choices
for choice in choices:
# Pruning: check if the choice is legal
# Pruning: check if the choice is valid
if is_valid(state, choice):
# Attempt: make a choice, update the state
# Attempt: make choice, update state
make_choice(state, choice)
# Proceed to the next round of selection
backtrack(state, [choice.left, choice.right], res)
# Retract: undo the choice, restore to the previous state
# Backtrack: undo choice, restore to previous state
undo_choice(state, choice)
@@ -66,6 +66,6 @@ if __name__ == "__main__":
res = []
backtrack(state=[], choices=[root], res=res)
print("\nOutput all root-to-node 7 paths, requiring paths not to include nodes with value 3")
print("\nOutput all paths from root node to node 7, excluding paths with nodes of value 3")
for path in res:
print([node.val for node in path])

View File

@@ -8,28 +8,28 @@ Author: krahets (krahets@163.com)
def backtrack(
state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]
):
"""Backtracking algorithm: Subset Sum I"""
"""Backtracking algorithm: Subset sum I"""
# When the subset sum equals target, record the solution
if target == 0:
res.append(list(state))
return
# Traverse all choices
# Pruning two: start traversing from start to avoid generating duplicate subsets
# Pruning 2: start traversing from start to avoid generating duplicate subsets
for i in range(start, len(choices)):
# Pruning one: if the subset sum exceeds target, end the loop immediately
# Pruning 1: if the subset sum exceeds target, end the loop directly
# This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
if target - choices[i] < 0:
break
# Attempt: make a choice, update target, start
# Attempt: make choice, update target, start
state.append(choices[i])
# Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i, res)
# Retract: undo the choice, restore to the previous state
# Backtrack: undo choice, restore to previous state
state.pop()
def subset_sum_i(nums: list[int], target: int) -> list[list[int]]:
"""Solve Subset Sum I"""
"""Solve subset sum I"""
state = [] # State (subset)
nums.sort() # Sort nums
start = 0 # Start point for traversal
@@ -45,4 +45,4 @@ if __name__ == "__main__":
res = subset_sum_i(nums, target)
print(f"Input array nums = {nums}, target = {target}")
print(f"All subsets equal to {target} res = {res}")
print(f"All subsets with sum equal to {target} res = {res}")

View File

@@ -12,26 +12,26 @@ def backtrack(
choices: list[int],
res: list[list[int]],
):
"""Backtracking algorithm: Subset Sum I"""
"""Backtracking algorithm: Subset sum I"""
# When the subset sum equals target, record the solution
if total == target:
res.append(list(state))
return
# Traverse all choices
for i in range(len(choices)):
# Pruning: if the subset sum exceeds target, skip that choice
# Pruning: if the subset sum exceeds target, skip this choice
if total + choices[i] > target:
continue
# Attempt: make a choice, update elements and total
# Attempt: make choice, update element sum total
state.append(choices[i])
# Proceed to the next round of selection
backtrack(state, target, total + choices[i], choices, res)
# Retract: undo the choice, restore to the previous state
# Backtrack: undo choice, restore to previous state
state.pop()
def subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:
"""Solve Subset Sum I (including duplicate subsets)"""
"""Solve subset sum I (including duplicate subsets)"""
state = [] # State (subset)
total = 0 # Subset sum
res = [] # Result list (subset list)
@@ -46,5 +46,5 @@ if __name__ == "__main__":
res = subset_sum_i_naive(nums, target)
print(f"Input array nums = {nums}, target = {target}")
print(f"All subsets equal to {target} res = {res}")
print(f"The result of this method includes duplicate sets")
print(f"All subsets with sum equal to {target} res = {res}")
print(f"Please note that the result output by this method contains duplicate sets")

View File

@@ -8,32 +8,32 @@ Author: krahets (krahets@163.com)
def backtrack(
state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]
):
"""Backtracking algorithm: Subset Sum II"""
"""Backtracking algorithm: Subset sum II"""
# When the subset sum equals target, record the solution
if target == 0:
res.append(list(state))
return
# Traverse all choices
# Pruning two: start traversing from start to avoid generating duplicate subsets
# Pruning three: start traversing from start to avoid repeatedly selecting the same element
# Pruning 2: start traversing from start to avoid generating duplicate subsets
# Pruning 3: start traversing from start to avoid repeatedly selecting the same element
for i in range(start, len(choices)):
# Pruning one: if the subset sum exceeds target, end the loop immediately
# Pruning 1: if the subset sum exceeds target, end the loop directly
# This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
if target - choices[i] < 0:
break
# Pruning four: if the element equals the left element, it indicates that the search branch is repeated, skip it
# Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly
if i > start and choices[i] == choices[i - 1]:
continue
# Attempt: make a choice, update target, start
# Attempt: make choice, update target, start
state.append(choices[i])
# Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i + 1, res)
# Retract: undo the choice, restore to the previous state
# Backtrack: undo choice, restore to previous state
state.pop()
def subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:
"""Solve Subset Sum II"""
"""Solve subset sum II"""
state = [] # State (subset)
nums.sort() # Sort nums
start = 0 # Start point for traversal
@@ -49,4 +49,4 @@ if __name__ == "__main__":
res = subset_sum_ii(nums, target)
print(f"Input array nums = {nums}, target = {target}")
print(f"All subsets equal to {target} res = {res}")
print(f"All subsets with sum equal to {target} res = {res}")