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,108 @@
=begin
File: array.rb
Created Time: 2024-03-18
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Random access element ###
def random_access(nums)
# Randomly select a number in the interval [0, nums.length)
random_index = Random.rand(0...nums.length)
# Retrieve and return the random element
nums[random_index]
end
### Extend array length ###
# Note: Ruby's Array is dynamic array, can be directly expanded
# For learning purposes, this function treats Array as fixed-length array
def extend(nums, enlarge)
# Initialize an array with extended length
res = Array.new(nums.length + enlarge, 0)
# Copy all elements from the original array to the new array
for i in 0...nums.length
res[i] = nums[i]
end
# Return the extended new array
res
end
### Insert element num at index in array ###
def insert(nums, num, index)
# Move all elements at and after index index backward by one position
for i in (nums.length - 1).downto(index + 1)
nums[i] = nums[i - 1]
end
# Assign num to the element at index index
nums[index] = num
end
### Delete element at index ###
def remove(nums, index)
# Move all elements after index index forward by one position
for i in index...(nums.length - 1)
nums[i] = nums[i + 1]
end
end
### Traverse array ###
def traverse(nums)
count = 0
# Traverse array by index
for i in 0...nums.length
count += nums[i]
end
# Direct traversal of array elements
for num in nums
count += num
end
end
### Find specified element in array ###
def find(nums, target)
for i in 0...nums.length
return i if nums[i] == target
end
-1
end
### Driver Code ###
if __FILE__ == $0
# Initialize array
arr = Array.new(5, 0)
puts "Array arr = #{arr}"
nums = [1, 3, 2, 5, 4]
puts "Array nums = #{nums}"
# Insert element
random_num = random_access(nums)
puts "Get random element #{random_num} from nums"
# Traverse array
nums = extend(nums, 3)
puts "Extend array length to 8, get nums = #{nums}"
# Insert element
insert(nums, 6, 3)
puts "Insert number 6 at index 3, get nums = #{nums}"
# Remove element
remove(nums, 2)
puts "Delete element at index 2, get nums = #{nums}"
# Traverse array
traverse(nums)
# Find element
index = find(nums, 3)
puts "Find element 3 in nums, index = #{index}"
end
@@ -0,0 +1,83 @@
=begin
File: linked_list.rb
Created Time: 2024-03-18
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/list_node'
require_relative '../utils/print_util'
### Insert node _p after node n0 in linked list ###
# Ruby's `p` is a built-in function, `P` is a constant, so use `_p` instead
def insert(n0, _p)
n1 = n0.next
_p.next = n1
n0.next = _p
end
### Delete first node after node n0 in linked list ###
def remove(n0)
return if n0.next.nil?
# n0 -> remove_node -> n1
remove_node = n0.next
n1 = remove_node.next
n0.next = n1
end
### Access node at index in linked list ###
def access(head, index)
for i in 0...index
return nil if head.nil?
head = head.next
end
head
end
### Find first node with value target in linked list ###
def find(head, target)
index = 0
while head
return index if head.val == target
head = head.next
index += 1
end
-1
end
### Driver Code ###
if __FILE__ == $0
# Initialize linked list
# Initialize each node
n0 = ListNode.new(1)
n1 = ListNode.new(3)
n2 = ListNode.new(2)
n3 = ListNode.new(5)
n4 = ListNode.new(4)
# Build references between nodes
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
puts "Initialized linked list is"
print_linked_list(n0)
# Insert node
insert(n0, ListNode.new(0))
print_linked_list n0
# Remove node
remove(n0)
puts "Linked list after removing node is"
print_linked_list(n0)
# Access node
node = access(n0, 3)
puts "Value of node at index 3 in linked list = #{node.val}"
# Search node
index = find(n0, 2)
puts "Index of node with value 2 in linked list = #{index}"
end
@@ -0,0 +1,60 @@
=begin
File: list.rb
Created Time: 2024-03-18
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Driver Code ###
if __FILE__ == $0
# Initialize list
nums = [1, 3, 2, 5, 4]
puts "List nums = #{nums}"
# Update element
num = nums[1]
puts "Access element at index 1, get num = #{num}"
# Add elements at the end
nums[1] = 0
puts "Update element at index 1 to 0, get nums = #{nums}"
# Remove element
nums.clear
puts "After clearing list, nums = #{nums}"
# Direct traversal of list elements
nums << 1
nums << 3
nums << 2
nums << 5
nums << 4
puts "After adding elements, nums = #{nums}"
# Sort list
nums.insert(3, 6)
puts "Insert element 6 at index 3, get nums = #{nums}"
# Remove element
nums.delete_at(3)
puts "Delete element at index 3, get nums = #{nums}"
# Traverse list by index
count = 0
for i in 0...nums.length
count += nums[i]
end
# Directly traverse list elements
count = 0
nums.each do |x|
count += x
end
# Concatenate two lists
nums1 = [6, 8, 7, 10, 9]
nums += nums1
puts "After concatenating list nums1 to nums, get nums = #{nums}"
nums = nums.sort { |a, b| a <=> b }
puts "After sorting list, nums = #{nums}"
end
@@ -0,0 +1,132 @@
=begin
File: my_list.rb
Created Time: 2024-03-18
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### List class ###
class MyList
attr_reader :size # Get list length (current number of elements)
attr_reader :capacity # Get list capacity
### Constructor ###
def initialize
@capacity = 10
@size = 0
@extend_ratio = 2
@arr = Array.new(capacity)
end
### Access element ###
def get(index)
# If the index is out of bounds, throw an exception, as below
raise IndexError, "Index out of bounds" if index < 0 || index >= size
@arr[index]
end
### Access element ###
def set(index, num)
raise IndexError, "Index out of bounds" if index < 0 || index >= size
@arr[index] = num
end
### Add element at end ###
def add(num)
# When the number of elements exceeds capacity, trigger the extension mechanism
extend_capacity if size == capacity
@arr[size] = num
# Update the number of elements
@size += 1
end
### Insert element in middle ###
def insert(index, num)
raise IndexError, "Index out of bounds" if index < 0 || index >= size
# When the number of elements exceeds capacity, trigger the extension mechanism
extend_capacity if size == capacity
# Move all elements after index index forward by one position
for j in (size - 1).downto(index)
@arr[j + 1] = @arr[j]
end
@arr[index] = num
# Update the number of elements
@size += 1
end
### Delete element ###
def remove(index)
raise IndexError, "Index out of bounds" if index < 0 || index >= size
num = @arr[index]
# Move all elements after index forward by one position
for j in index...size
@arr[j] = @arr[j + 1]
end
# Update the number of elements
@size -= 1
# Return the removed element
num
end
### Expand list capacity ###
def extend_capacity
# Create new array with length extend_ratio times original, copy original array to new array
arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))
# Add elements at the end
@capacity = arr.length
end
### Convert list to array ###
def to_array
sz = size
# Elements enqueue
arr = Array.new(sz)
for i in 0...sz
arr[i] = get(i)
end
arr
end
end
### Driver Code ###
if __FILE__ == $0
# Initialize list
nums = MyList.new
# Direct traversal of list elements
nums.add(1)
nums.add(3)
nums.add(2)
nums.add(5)
nums.add(4)
puts "List nums = #{nums.to_array}, capacity = #{nums.capacity}, length = #{nums.size}"
# Sort list
nums.insert(3, 6)
puts "Insert number 6 at index 3, get nums = #{nums.to_array}"
# Remove element
nums.remove(3)
puts "Delete element at index 3, get nums = #{nums.to_array}"
# Update element
num = nums.get(1)
puts "Access element at index 1, get num = #{num}"
# Add elements at the end
nums.set(1, 0)
puts "Update element at index 1 to 0, get nums = #{nums.to_array}"
# Test capacity expansion mechanism
for i in 0...10
# At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
nums.add(i)
end
puts "After expansion, list nums = #{nums.to_array}, capacity = #{nums.capacity}, length = #{nums.size}"
end