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,74 @@
/*
* File: iteration.rs
* Created Time: 2023-09-02
* Author: night-cruise (2586447362@qq.com)
*/
/* for loop */
fn for_loop(n: i32) -> i32 {
let mut res = 0;
// Sum 1, 2, ..., n-1, n
for i in 1..=n {
res += i;
}
res
}
/* while loop */
fn while_loop(n: i32) -> i32 {
let mut res = 0;
let mut i = 1; // Initialize condition variable
// Sum 1, 2, ..., n-1, n
while i <= n {
res += i;
i += 1; // Update condition variable
}
res
}
/* while loop (two updates) */
fn while_loop_ii(n: i32) -> i32 {
let mut res = 0;
let mut i = 1; // Initialize condition variable
// Sum 1, 4, 10, ...
while i <= n {
res += i;
// Update condition variable
i += 1;
i *= 2;
}
res
}
/* Nested for loop */
fn nested_for_loop(n: i32) -> String {
let mut res = vec![];
// Loop i = 1, 2, ..., n-1, n
for i in 1..=n {
// Loop j = 1, 2, ..., n-1, n
for j in 1..=n {
res.push(format!("({}, {}), ", i, j));
}
}
res.join("")
}
/* Driver Code */
fn main() {
let n = 5;
let mut res;
res = for_loop(n);
println!("\nFor loop sum result res = {res}");
res = while_loop(n);
println!("\nWhile loop sum result res = {res}");
res = while_loop_ii(n);
println!("\nWhile loop (two updates) sum result res = {}", res);
let res = nested_for_loop(n);
println!("\nNested for loop traversal result {res}");
}
@@ -0,0 +1,76 @@
/*
* File: recursion.rs
* Created Time: 2023-09-02
* Author: night-cruise (2586447362@qq.com)
*/
/* Recursion */
fn recur(n: i32) -> i32 {
// Termination condition
if n == 1 {
return 1;
}
// Recurse: recursive call
let res = recur(n - 1);
// Return: return result
n + res
}
/* Simulate recursion using iteration */
fn for_loop_recur(n: i32) -> i32 {
// Use an explicit stack to simulate the system call stack
let mut stack = Vec::new();
let mut res = 0;
// Recurse: recursive call
for i in (1..=n).rev() {
// Simulate "recurse" with "push"
stack.push(i);
}
// Return: return result
while !stack.is_empty() {
// Simulate "return" with "pop"
res += stack.pop().unwrap();
}
// res = 1+2+3+...+n
res
}
/* Tail recursion */
fn tail_recur(n: i32, res: i32) -> i32 {
// Termination condition
if n == 0 {
return res;
}
// Tail recursive call
tail_recur(n - 1, res + n)
}
/* Fibonacci sequence: recursion */
fn fib(n: i32) -> i32 {
// Termination condition f(1) = 0, f(2) = 1
if n == 1 || n == 2 {
return n - 1;
}
// Recursive call f(n) = f(n-1) + f(n-2)
let res = fib(n - 1) + fib(n - 2);
// Return result
res
}
/* Driver Code */
fn main() {
let n = 5;
let mut res;
res = recur(n);
println!("\nRecursion sum result res = {res}");
res = for_loop_recur(n);
println!("\nUsing iteration to simulate recursion sum result res = {res}");
res = tail_recur(n, 0);
println!("\nTail recursion sum result res = {res}");
res = fib(n);
println!("\nThe {n}th Fibonacci number is {res}");
}
@@ -0,0 +1,114 @@
/*
* File: space_complexity.rs
* Created Time: 2023-03-11
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::{print_util, ListNode, TreeNode};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
/* Function */
fn function() -> i32 {
// Perform some operations
return 0;
}
/* Constant order */
#[allow(unused)]
fn constant(n: i32) {
// Constants, variables, objects occupy O(1) space
const A: i32 = 0;
let b = 0;
let nums = vec![0; 10000];
let node = ListNode::new(0);
// Variables in the loop occupy O(1) space
for i in 0..n {
let c = 0;
}
// Functions in the loop occupy O(1) space
for i in 0..n {
function();
}
}
/* Linear order */
#[allow(unused)]
fn linear(n: i32) {
// Array of length n uses O(n) space
let mut nums = vec![0; n as usize];
// A list of length n occupies O(n) space
let mut nodes = Vec::new();
for i in 0..n {
nodes.push(ListNode::new(i))
}
// A hash table of length n occupies O(n) space
let mut map = HashMap::new();
for i in 0..n {
map.insert(i, i.to_string());
}
}
/* Linear order (recursive implementation) */
fn linear_recur(n: i32) {
println!("Recursion n = {}", n);
if n == 1 {
return;
};
linear_recur(n - 1);
}
/* Exponential order */
#[allow(unused)]
fn quadratic(n: i32) {
// Matrix uses O(n^2) space
let num_matrix = vec![vec![0; n as usize]; n as usize];
// 2D list uses O(n^2) space
let mut num_list = Vec::new();
for i in 0..n {
let mut tmp = Vec::new();
for j in 0..n {
tmp.push(0);
}
num_list.push(tmp);
}
}
/* Quadratic order (recursive implementation) */
fn quadratic_recur(n: i32) -> i32 {
if n <= 0 {
return 0;
};
// Array nums has length n, n-1, ..., 2, 1
let nums = vec![0; n as usize];
println!("In recursion n = {}, nums length = {}", n, nums.len());
return quadratic_recur(n - 1);
}
/* Driver Code */
fn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {
if n == 0 {
return None;
};
let root = TreeNode::new(0);
root.borrow_mut().left = build_tree(n - 1);
root.borrow_mut().right = build_tree(n - 1);
return Some(root);
}
/* Driver Code */
fn main() {
let n = 5;
// Constant order
constant(n);
// Linear order
linear(n);
linear_recur(n);
// Exponential order
quadratic(n);
quadratic_recur(n);
// Exponential order
let root = build_tree(n);
print_util::print_tree(&root.unwrap());
}
@@ -0,0 +1,170 @@
/*
* File: time_complexity.rs
* Created Time: 2023-01-10
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
*/
/* Constant order */
fn constant(n: i32) -> i32 {
_ = n;
let mut count = 0;
let size = 100_000;
for _ in 0..size {
count += 1;
}
count
}
/* Linear order */
fn linear(n: i32) -> i32 {
let mut count = 0;
for _ in 0..n {
count += 1;
}
count
}
/* Linear order (traversing array) */
fn array_traversal(nums: &[i32]) -> i32 {
let mut count = 0;
// Number of iterations is proportional to the array length
for _ in nums {
count += 1;
}
count
}
/* Exponential order */
fn quadratic(n: i32) -> i32 {
let mut count = 0;
// Number of iterations is quadratically related to the data size n
for _ in 0..n {
for _ in 0..n {
count += 1;
}
}
count
}
/* Quadratic order (bubble sort) */
fn bubble_sort(nums: &mut [i32]) -> i32 {
let mut count = 0; // Counter
// Outer loop: unsorted range is [0, i]
for i in (1..nums.len()).rev() {
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for j in 0..i {
if nums[j] > nums[j + 1] {
// Swap nums[j] and nums[j + 1]
let tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // Element swap includes 3 unit operations
}
}
}
count
}
/* Exponential order (loop implementation) */
fn exponential(n: i32) -> i32 {
let mut count = 0;
let mut base = 1;
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
for _ in 0..n {
for _ in 0..base {
count += 1
}
base *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
count
}
/* Exponential order (recursive implementation) */
fn exp_recur(n: i32) -> i32 {
if n == 1 {
return 1;
}
exp_recur(n - 1) + exp_recur(n - 1) + 1
}
/* Logarithmic order (loop implementation) */
fn logarithmic(mut n: i32) -> i32 {
let mut count = 0;
while n > 1 {
n = n / 2;
count += 1;
}
count
}
/* Logarithmic order (recursive implementation) */
fn log_recur(n: i32) -> i32 {
if n <= 1 {
return 0;
}
log_recur(n / 2) + 1
}
/* Linearithmic order */
fn linear_log_recur(n: i32) -> i32 {
if n <= 1 {
return 1;
}
let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);
for _ in 0..n {
count += 1;
}
return count;
}
/* Factorial order (recursive implementation) */
fn factorial_recur(n: i32) -> i32 {
if n == 0 {
return 1;
}
let mut count = 0;
// Split from 1 into n
for _ in 0..n {
count += factorial_recur(n - 1);
}
count
}
/* Driver Code */
fn main() {
// You can modify n to run and observe the trend of the number of operations for various complexities
let n: i32 = 8;
println!("Input data size n = {}", n);
let mut count = constant(n);
println!("Constant-time operations count = {}", count);
count = linear(n);
println!("Linear-time operations count = {}", count);
count = array_traversal(&vec![0; n as usize]);
println!("Linear-time (array traversal) operations count = {}", count);
count = quadratic(n);
println!("Quadratic-time operations count = {}", count);
let mut nums = (1..=n).rev().collect::<Vec<_>>(); // [n,n-1,...,2,1]
count = bubble_sort(&mut nums);
println!("Quadratic-time (bubble sort) operations count = {}", count);
count = exponential(n);
println!("Exponential-time (iterative) operations count = {}", count);
count = exp_recur(n);
println!("Exponential-time (recursive) operations count = {}", count);
count = logarithmic(n);
println!("Logarithmic-time (iterative) operations count = {}", count);
count = log_recur(n);
println!("Logarithmic-time (recursive) operations count = {}", count);
count = linear_log_recur(n);
println!("Linearithmic-time (recursive) operations count = {}", count);
count = factorial_recur(n);
println!("Factorial-time (recursive) operations count = {}", count);
}
@@ -0,0 +1,42 @@
/*
* File: worst_best_time_complexity.rs
* Created Time: 2023-01-13
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
use rand::seq::SliceRandom;
use rand::thread_rng;
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
fn random_numbers(n: i32) -> Vec<i32> {
// Generate array nums = { 1, 2, 3, ..., n }
let mut nums = (1..=n).collect::<Vec<i32>>();
// Randomly shuffle array elements
nums.shuffle(&mut thread_rng());
nums
}
/* Find the index of number 1 in array nums */
fn find_one(nums: &[i32]) -> Option<usize> {
for i in 0..nums.len() {
// When element 1 is at the head of the array, best time complexity O(1) is achieved
// When element 1 is at the tail of the array, worst time complexity O(n) is achieved
if nums[i] == 1 {
return Some(i);
}
}
None
}
/* Driver Code */
fn main() {
for _ in 0..10 {
let n = 100;
let nums = random_numbers(n);
let index = find_one(&nums).unwrap();
print!("\nArray [ 1, 2, ..., n ] after shuffling = ");
print_util::print_array(&nums);
println!("\nIndex of number 1 is {}", index);
}
}