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,111 @@
/*
* File: array.rs
* Created Time: 2023-01-15
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
use rand::Rng;
/* Random access to element */
fn random_access(nums: &[i32]) -> i32 {
// Randomly select a number in interval [0, nums.len())
let random_index = rand::thread_rng().gen_range(0..nums.len());
// Retrieve and return the random element
let random_num = nums[random_index];
random_num
}
/* Extend array length */
fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {
// Initialize an array with extended length
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
// Copy all elements from original array to new
res[0..nums.len()].copy_from_slice(nums);
// Return the extended new array
res
}
/* Insert element num at index index in the array */
fn insert(nums: &mut [i32], num: i32, index: usize) {
// Move all elements at and after index index backward by one position
for i in (index + 1..nums.len()).rev() {
nums[i] = nums[i - 1];
}
// Assign num to the element at index index
nums[index] = num;
}
/* Remove the element at index index */
fn remove(nums: &mut [i32], index: usize) {
// Move all elements after index index forward by one position
for i in index..nums.len() - 1 {
nums[i] = nums[i + 1];
}
}
/* Traverse array */
fn traverse(nums: &[i32]) {
let mut _count = 0;
// Traverse array by index
for i in 0..nums.len() {
_count += nums[i];
}
// Direct traversal of array elements
_count = 0;
for &num in nums {
_count += num;
}
}
/* Find the specified element in the array */
fn find(nums: &[i32], target: i32) -> Option<usize> {
for i in 0..nums.len() {
if nums[i] == target {
return Some(i);
}
}
None
}
/* Driver Code */
fn main() {
/* Initialize array */
let arr: [i32; 5] = [0; 5];
print!("Array arr = ");
print_util::print_array(&arr);
// In Rust, specifying length ([i32; 5]) is an array, without length (&[i32]) is a slice
// Since Rust arrays are designed to have compile-time determined length, only constants can specify length
// Vector is the type Rust generally uses as a dynamic array
// To facilitate implementing the extend() method, the following treats vector as array
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
print!("\nArray nums = ");
print_util::print_array(&nums);
// Insert element
let random_num = random_access(&nums);
println!("\nGet random element {} from nums", random_num);
// Traverse array
let mut nums: Vec<i32> = extend(&nums, 3);
print!("Extend array length to 8, resulting in nums = ");
print_util::print_array(&nums);
// Insert element
insert(&mut nums, 6, 3);
print!("\nInsert number 6 at index 3, get nums = ");
print_util::print_array(&nums);
// Remove element
remove(&mut nums, 2);
print!("\nDelete element at index 2, get nums = ");
print_util::print_array(&nums);
// Traverse array
traverse(&nums);
// Find element
let index = find(&nums, 3).unwrap();
println!("\nFind element 3 in nums, index = {}", index);
}
@@ -0,0 +1,100 @@
/*
* File: linked_list.rs
* Created Time: 2023-03-05
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::{print_util, ListNode};
use std::cell::RefCell;
use std::rc::Rc;
/* Insert node P after node n0 in the linked list */
#[allow(non_snake_case)]
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
let n1 = n0.borrow_mut().next.take();
P.borrow_mut().next = n1;
n0.borrow_mut().next = Some(P);
}
/* Remove the first node after node n0 in the linked list */
#[allow(non_snake_case)]
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
// n0 -> P -> n1
let P = n0.borrow_mut().next.take();
if let Some(node) = P {
let n1 = node.borrow_mut().next.take();
n0.borrow_mut().next = n1;
}
}
/* Access the node at index index in the linked list */
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {
fn dfs<T>(
head: Option<&Rc<RefCell<ListNode<T>>>>,
index: i32,
) -> Option<Rc<RefCell<ListNode<T>>>> {
if index <= 0 {
return head.cloned();
}
if let Some(node) = head {
dfs(node.borrow().next.as_ref(), index - 1)
} else {
None
}
}
dfs(Some(head).as_ref(), index)
}
/* Find the first node with value target in the linked list */
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {
fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {
if let Some(node) = head {
if node.borrow().val == target {
return idx;
}
return find(node.borrow().next.as_ref(), target, idx + 1);
} else {
-1
}
}
find(Some(head).as_ref(), target, 0)
}
/* Driver Code */
fn main() {
/* Initialize linked list */
// Initialize each node
let n0 = ListNode::new(1);
let n1 = ListNode::new(3);
let n2 = ListNode::new(2);
let n3 = ListNode::new(5);
let n4 = ListNode::new(4);
// Build references between nodes
n0.borrow_mut().next = Some(n1.clone());
n1.borrow_mut().next = Some(n2.clone());
n2.borrow_mut().next = Some(n3.clone());
n3.borrow_mut().next = Some(n4.clone());
print!("Initialized linked list is ");
print_util::print_linked_list(&n0);
/* Insert node */
insert(&n0, ListNode::new(0));
print!("After inserting node, linked list is ");
print_util::print_linked_list(&n0);
/* Remove node */
remove(&n0);
print!("After deleting node, linked list is ");
print_util::print_linked_list(&n0);
/* Access node */
let node = access(n0.clone(), 3);
println!("Value of node at index 3 in linked list = {}", node.unwrap().borrow().val);
/* Search node */
let index = find(n0.clone(), 2);
println!("Index of node with value 2 in linked list = {}", index);
}
@@ -0,0 +1,71 @@
/*
* File: list.rs
* Created Time: 2023-01-18
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
/* Driver Code */
fn main() {
// Initialize list
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
print!("List nums = ");
print_util::print_array(&nums);
// Update element
let num = nums[1];
println!("\nAccess element at index 1, get num = {num}");
// Add elements at the end
nums[1] = 0;
print!("Update element at index 1 to 0, resulting in nums = ");
print_util::print_array(&nums);
// Remove element
nums.clear();
print!("\nAfter clearing list, nums = ");
print_util::print_array(&nums);
// Direct traversal of list elements
nums.push(1);
nums.push(3);
nums.push(2);
nums.push(5);
nums.push(4);
print!("\nAfter adding elements, nums = ");
print_util::print_array(&nums);
// Sort list
nums.insert(3, 6);
print!("\nInsert number 6 at index 3, get nums = ");
print_util::print_array(&nums);
// Remove element
nums.remove(3);
print!("\nDelete element at index 3, get nums = ");
print_util::print_array(&nums);
// Traverse list by index
let mut _count = 0;
for i in 0..nums.len() {
_count += nums[i];
}
// Directly traverse list elements
_count = 0;
for x in &nums {
_count += x;
}
// Concatenate two lists
let mut nums1 = vec![6, 8, 7, 10, 9];
nums.append(&mut nums1); // After append (move), nums1 is empty!
// nums.extend(&nums1); // extend (borrow) allows nums1 to continue being used
print!("\nAfter concatenating list nums1 to nums, get nums = ");
print_util::print_array(&nums);
// Sort list
nums.sort();
print!("\nAfter sorting list, nums = ");
print_util::print_array(&nums);
}
@@ -0,0 +1,164 @@
/*
* File: my_list.rs
* Created Time: 2023-03-11
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
/* List class */
#[allow(dead_code)]
struct MyList {
arr: Vec<i32>, // Array (stores list elements)
capacity: usize, // List capacity
size: usize, // List length (current number of elements)
extend_ratio: usize, // Multiple by which the list capacity is extended each time
}
#[allow(unused, unused_comparisons)]
impl MyList {
/* Constructor */
pub fn new(capacity: usize) -> Self {
let mut vec = vec![0; capacity];
Self {
arr: vec,
capacity,
size: 0,
extend_ratio: 2,
}
}
/* Get list length (current number of elements) */
pub fn size(&self) -> usize {
return self.size;
}
/* Get list capacity */
pub fn capacity(&self) -> usize {
return self.capacity;
}
/* Update element */
pub fn get(&self, index: usize) -> i32 {
// If the index is out of bounds, throw an exception, as below
if index >= self.size {
panic!("Index out of bounds")
};
return self.arr[index];
}
/* Add elements at the end */
pub fn set(&mut self, index: usize, num: i32) {
if index >= self.size {
panic!("Index out of bounds")
};
self.arr[index] = num;
}
/* Direct traversal of list elements */
pub fn add(&mut self, num: i32) {
// When the number of elements exceeds capacity, trigger the extension mechanism
if self.size == self.capacity() {
self.extend_capacity();
}
self.arr[self.size] = num;
// Update the number of elements
self.size += 1;
}
/* Sort list */
pub fn insert(&mut self, index: usize, num: i32) {
if index >= self.size() {
panic!("Index out of bounds")
};
// When the number of elements exceeds capacity, trigger the extension mechanism
if self.size == self.capacity() {
self.extend_capacity();
}
// Move all elements after index index forward by one position
for j in (index..self.size).rev() {
self.arr[j + 1] = self.arr[j];
}
self.arr[index] = num;
// Update the number of elements
self.size += 1;
}
/* Remove element */
pub fn remove(&mut self, index: usize) -> i32 {
if index >= self.size() {
panic!("Index out of bounds")
};
let num = self.arr[index];
// Create a new array with length _extend_ratio times the original array, and copy the original array to the new array
for j in index..self.size - 1 {
self.arr[j] = self.arr[j + 1];
}
// Update the number of elements
self.size -= 1;
// Return the removed element
return num;
}
/* Driver Code */
pub fn extend_capacity(&mut self) {
// Create new array with length extend_ratio times original, copy original array to new array
let new_capacity = self.capacity * self.extend_ratio;
self.arr.resize(new_capacity, 0);
// Add elements at the end
self.capacity = new_capacity;
}
/* Convert list to array */
pub fn to_array(&self) -> Vec<i32> {
// Elements enqueue
let mut arr = Vec::new();
for i in 0..self.size {
arr.push(self.get(i));
}
arr
}
}
/* Driver Code */
fn main() {
/* Initialize list */
let mut nums = MyList::new(10);
/* Direct traversal of list elements */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
print!("List nums = ");
print_util::print_array(&nums.to_array());
print!(", capacity = {}, length = {}", nums.capacity(), nums.size());
/* Sort list */
nums.insert(3, 6);
print!("\nInsert number 6 at index 3, get nums = ");
print_util::print_array(&nums.to_array());
/* Remove element */
nums.remove(3);
print!("\nDelete element at index 3, get nums = ");
print_util::print_array(&nums.to_array());
/* Update element */
let num = nums.get(1);
println!("\nAccess element at index 1, get num = {num}");
/* Add elements at the end */
nums.set(1, 0);
print!("Update element at index 1 to 0, resulting in nums = ");
print_util::print_array(&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);
}
print!("\nAfter expanding list, nums = ");
print_util::print_array(&nums.to_array());
print!(", capacity = {}, length = {}", nums.capacity(), nums.size());
}