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
@@ -0,0 +1,61 @@
=begin
File: n_queens.rb
Created Time: 2024-05-21
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Backtracking: n queens ###
def backtrack(row, n, state, res, cols, diags1, diags2)
# When all rows are placed, record the solution
if row == n
res << state.map { |row| row.dup }
return
end
# Traverse all columns
for col in 0...n
# Calculate the main diagonal and anti-diagonal corresponding to this cell
diag1 = row - col + n - 1
diag2 = row + col
# Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
if !cols[col] && !diags1[diag1] && !diags2[diag2]
# 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)
# Backtrack: restore this cell to an empty cell
state[row][col] = "#"
cols[col] = diags1[diag1] = diags2[diag2] = false
end
end
end
### Solve n queens ###
def n_queens(n)
# Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
state = Array.new(n) { Array.new(n, "#") }
cols = Array.new(n, false) # Record whether there is a queen in the column
diags1 = Array.new(2 * n - 1, false) # Record whether there is a queen on the main diagonal
diags2 = Array.new(2 * n - 1, false) # Record whether there is a queen on the anti-diagonal
res = []
backtrack(0, n, state, res, cols, diags1, diags2)
res
end
### Driver Code ###
if __FILE__ == $0
n = 4
res = n_queens(n)
puts "Input board size is #{n}"
puts "Total queen placement solutions: #{res.length}"
for state in res
puts "--------------------"
for row in state
p row
end
end
end
@@ -0,0 +1,46 @@
=begin
File: permutations_i.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Backtracking: permutations I ###
def backtrack(state, choices, selected, res)
# When the state length equals the number of elements, record the solution
if state.length == choices.length
res << state.dup
return
end
# Traverse all choices
choices.each_with_index do |choice, i|
# Pruning: do not allow repeated selection of elements
unless selected[i]
# Attempt: make choice, update state
selected[i] = true
state << choice
# Proceed to the next round of selection
backtrack(state, choices, selected, res)
# Backtrack: undo choice, restore to previous state
selected[i] = false
state.pop
end
end
end
### Permutations I ###
def permutations_i(nums)
res = []
backtrack([], nums, Array.new(nums.length, false), res)
res
end
### Driver Code ###
if __FILE__ == $0
nums = [1, 2, 3]
res = permutations_i(nums)
puts "Input array nums = #{nums}"
puts "All permutations res = #{res}"
end
@@ -0,0 +1,48 @@
=begin
File: permutations_ii.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Backtracking: permutations II ###
def backtrack(state, choices, selected, res)
# When the state length equals the number of elements, record the solution
if state.length == choices.length
res << state.dup
return
end
# Traverse all choices
duplicated = Set.new
choices.each_with_index do |choice, i|
# Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if !selected[i] && !duplicated.include?(choice)
# Attempt: make choice, update state
duplicated.add(choice)
selected[i] = true
state << choice
# Proceed to the next round of selection
backtrack(state, choices, selected, res)
# Backtrack: undo choice, restore to previous state
selected[i] = false
state.pop
end
end
end
### Permutations II ###
def permutations_ii(nums)
res = []
backtrack([], nums, Array.new(nums.length, false), res)
res
end
### Driver Code ###
if __FILE__ == $0
nums = [1, 2, 2]
res = permutations_ii(nums)
puts "Input array nums = #{nums}"
puts "All permutations res = #{res}"
end
@@ -0,0 +1,33 @@
=begin
File: preorder_traversal_i_compact.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/tree_node'
require_relative '../utils/print_util'
### Pre-order traversal: example 1 ###
def pre_order(root)
return unless root
# Record solution
$res << root if root.val == 7
pre_order(root.left)
pre_order(root.right)
end
### Driver Code ###
if __FILE__ == $0
root = arr_to_tree([1, 7, 3, 4, 5, 6, 7])
puts "\nInitialize binary tree"
print_tree(root)
# Preorder traversal
$res = []
pre_order(root)
puts "\nOutput all nodes with value 7"
p $res.map { |node| node.val }
end
@@ -0,0 +1,41 @@
=begin
File: preorder_traversal_ii_compact.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/tree_node'
require_relative '../utils/print_util'
### Pre-order traversal: example 2 ###
def pre_order(root)
return unless root
# Attempt
$path << root
# Record solution
$res << $path.dup if root.val == 7
pre_order(root.left)
pre_order(root.right)
# Backtrack
$path.pop
end
### Driver Code ###
if __FILE__ == $0
root = arr_to_tree([1, 7, 3, 4, 5, 6, 7])
puts "\nInitialize binary tree"
print_tree(root)
# Preorder traversal
$path, $res = [], []
pre_order(root)
puts "\nOutput all paths from root node to node 7"
for path in $res
p path.map { |node| node.val }
end
end
@@ -0,0 +1,42 @@
=begin
File: preorder_traversal_iii_compact.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/tree_node'
require_relative '../utils/print_util'
### Pre-order traversal: example 3 ###
def pre_order(root)
# Pruning
return if !root || root.val == 3
# Attempt
$path.append(root)
# Record solution
$res << $path.dup if root.val == 7
pre_order(root.left)
pre_order(root.right)
# Backtrack
$path.pop
end
### Driver Code ###
if __FILE__ == $0
root = arr_to_tree([1, 7, 3, 4, 5, 6, 7])
puts "\nInitialize binary tree"
print_tree(root)
# Preorder traversal
$path, $res = [], []
pre_order(root)
puts "\nOutput all paths from root node to node 7, paths do not include nodes with value 3"
for path in $res
p path.map { |node| node.val }
end
end
@@ -0,0 +1,68 @@
=begin
File: preorder_traversal_iii_template.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/tree_node'
require_relative '../utils/print_util'
### Check if current state is solution ###
def is_solution?(state)
!state.empty? && state.last.val == 7
end
### Record solution ###
def record_solution(state, res)
res << state.dup
end
### Check if choice is valid in current state ###
def is_valid?(state, choice)
choice && choice.val != 3
end
### Update state ###
def make_choice(state, choice)
state << choice
end
### Restore state ###
def undo_choice(state, choice)
state.pop
end
### Backtracking: example 3 ###
def backtrack(state, choices, res)
# Check if it is a solution
record_solution(state, res) if is_solution?(state)
# Traverse all choices
for choice in choices
# Pruning: check if the choice is valid
if is_valid?(state, choice)
# Attempt: make choice, update state
make_choice(state, choice)
# Proceed to the next round of selection
backtrack(state, [choice.left, choice.right], res)
# Backtrack: undo choice, restore to previous state
undo_choice(state, choice)
end
end
end
### Driver Code ###
if __FILE__ == $0
root = arr_to_tree([1, 7, 3, 4, 5, 6, 7])
puts "\nInitialize binary tree"
print_tree(root)
# Backtracking algorithm
res = []
backtrack([], [root], res)
puts "\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3"
for path in res
p path.map { |node| node.val }
end
end
@@ -0,0 +1,47 @@
=begin
File: subset_sum_i.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Backtracking: subset sum I ###
def backtrack(state, target, choices, start, res)
# When the subset sum equals target, record the solution
if target.zero?
res << state.dup
return
end
# Traverse all choices
# Pruning 2: start traversing from start to avoid generating duplicate subsets
for i in start...choices.length
# 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
break if target - choices[i] < 0
# Attempt: make choice, update target, start
state << choices[i]
# Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i, res)
# Backtrack: undo choice, restore to previous state
state.pop
end
end
### Solve subset sum I ###
def subset_sum_i(nums, target)
state = [] # State (subset)
nums.sort! # Sort nums
start = 0 # Start point for traversal
res = [] # Result list (subset list)
backtrack(state, target, nums, start, res)
res
end
### Driver Code ###
if __FILE__ == $0
nums = [3, 4, 5]
target = 9
res = subset_sum_i(nums, target)
puts "Input array = #{nums}, target = #{target}"
puts "All subsets with sum equal to #{target} res = #{res}"
end
@@ -0,0 +1,46 @@
=begin
File: subset_sum_i_naive.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Backtracking: subset sum I ###
def backtrack(state, target, total, choices, res)
# When the subset sum equals target, record the solution
if total == target
res << state.dup
return
end
# Traverse all choices
for i in 0...choices.length
# Pruning: if the subset sum exceeds target, skip this choice
next if total + choices[i] > target
# Attempt: make choice, update element sum total
state << choices[i]
# Proceed to the next round of selection
backtrack(state, target, total + choices[i], choices, res)
# Backtrack: undo choice, restore to previous state
state.pop
end
end
### Solve subset sum I (with duplicate subsets) ###
def subset_sum_i_naive(nums, target)
state = [] # State (subset)
total = 0 # Subset sum
res = [] # Result list (subset list)
backtrack(state, target, total, nums, res)
res
end
### Driver Code ###
if __FILE__ == $0
nums = [3, 4, 5]
target = 9
res = subset_sum_i_naive(nums, target)
puts "Input array nums = #{nums}, target = #{target}"
puts "All subsets with sum equal to #{target} res = #{res}"
puts "Please note that this method outputs results containing duplicate sets"
end
@@ -0,0 +1,51 @@
=begin
File: subset_sum_ii.rb
Created Time: 2024-05-22
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Backtracking: subset sum II ###
def backtrack(state, target, choices, start, res)
# When the subset sum equals target, record the solution
if target.zero?
res << state.dup
return
end
# Traverse all choices
# 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 start...choices.length
# 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
break if target - choices[i] < 0
# Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly
next if i > start && choices[i] == choices[i - 1]
# Attempt: make choice, update target, start
state << choices[i]
# Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i + 1, res)
# Backtrack: undo choice, restore to previous state
state.pop
end
end
### Solve subset sum II ###
def subset_sum_ii(nums, target)
state = [] # State (subset)
nums.sort! # Sort nums
start = 0 # Start point for traversal
res = [] # Result list (subset list)
backtrack(state, target, nums, start, res)
res
end
### Driver Code ###
if __FILE__ == $0
nums = [4, 4, 5]
target = 9
res = subset_sum_ii(nums, target)
puts "Input array nums = #{nums}, target = #{target}"
puts "All subsets with sum equal to #{target} res = #{res}"
end