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

@@ -0,0 +1,77 @@
/**
* File: iteration.cs
* Created Time: 2023-08-28
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class iteration {
/* for loop */
int ForLoop(int n) {
int res = 0;
// Sum 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
res += i;
}
return res;
}
/* while loop */
int WhileLoop(int n) {
int res = 0;
int i = 1; // Initialize condition variable
// Sum 1, 2, ..., n-1, n
while (i <= n) {
res += i;
i += 1; // Update condition variable
}
return res;
}
/* while loop (two updates) */
int WhileLoopII(int n) {
int res = 0;
int i = 1; // Initialize condition variable
// Sum 1, 4, 10, ...
while (i <= n) {
res += i;
// Update condition variable
i += 1;
i *= 2;
}
return res;
}
/* Nested for loop */
string NestedForLoop(int n) {
StringBuilder res = new();
// Loop i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
// Loop j = 1, 2, ..., n-1, n
for (int j = 1; j <= n; j++) {
res.Append($"({i}, {j}), ");
}
}
return res.ToString();
}
/* Driver Code */
[Test]
public void Test() {
int n = 5;
int res;
res = ForLoop(n);
Console.WriteLine("\nfor loop sum result res = " + res);
res = WhileLoop(n);
Console.WriteLine("\nwhile loop sum result res = " + res);
res = WhileLoopII(n);
Console.WriteLine("\nwhile loop (two updates) sum result res = " + res);
string resStr = NestedForLoop(n);
Console.WriteLine("\nDouble for loop traversal result " + resStr);
}
}

View File

@@ -0,0 +1,78 @@
/**
* File: recursion.cs
* Created Time: 2023-08-28
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class recursion {
/* Recursion */
int Recur(int n) {
// Termination condition
if (n == 1)
return 1;
// Recurse: recursive call
int res = Recur(n - 1);
// Return: return result
return n + res;
}
/* Simulate recursion using iteration */
int ForLoopRecur(int n) {
// Use an explicit stack to simulate the system call stack
Stack<int> stack = new();
int res = 0;
// Recurse: recursive call
for (int i = n; i > 0; i--) {
// Simulate "recurse" with "push"
stack.Push(i);
}
// Return: return result
while (stack.Count > 0) {
// Simulate "return" with "pop"
res += stack.Pop();
}
// res = 1+2+3+...+n
return res;
}
/* Tail recursion */
int TailRecur(int n, int res) {
// Termination condition
if (n == 0)
return res;
// Tail recursive call
return TailRecur(n - 1, res + n);
}
/* Fibonacci sequence: recursion */
int Fib(int n) {
// 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)
int res = Fib(n - 1) + Fib(n - 2);
// Return result f(n)
return res;
}
/* Driver Code */
[Test]
public void Test() {
int n = 5;
int res;
res = Recur(n);
Console.WriteLine("\nRecursive function sum result res = " + res);
res = ForLoopRecur(n);
Console.WriteLine("\nUsing iteration to simulate recursive sum result res = " + res);
res = TailRecur(n, 0);
Console.WriteLine("\nTail recursive function sum result res = " + res);
res = Fib(n);
Console.WriteLine("\nThe " + n + "th term of the Fibonacci sequence is " + res);
}
}

View File

@@ -0,0 +1,104 @@
/**
* File: space_complexity.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class space_complexity {
/* Function */
int Function() {
// Perform some operations
return 0;
}
/* Constant order */
void Constant(int n) {
// Constants, variables, objects occupy O(1) space
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new(0);
// Variables in the loop occupy O(1) space
for (int i = 0; i < n; i++) {
int c = 0;
}
// Functions in the loop occupy O(1) space
for (int i = 0; i < n; i++) {
Function();
}
}
/* Linear order */
void Linear(int n) {
// Array of length n uses O(n) space
int[] nums = new int[n];
// A list of length n occupies O(n) space
List<ListNode> nodes = [];
for (int i = 0; i < n; i++) {
nodes.Add(new ListNode(i));
}
// A hash table of length n occupies O(n) space
Dictionary<int, string> map = [];
for (int i = 0; i < n; i++) {
map.Add(i, i.ToString());
}
}
/* Linear order (recursive implementation) */
void LinearRecur(int n) {
Console.WriteLine("Recursion n = " + n);
if (n == 1) return;
LinearRecur(n - 1);
}
/* Exponential order */
void Quadratic(int n) {
// Matrix uses O(n^2) space
int[,] numMatrix = new int[n, n];
// 2D list uses O(n^2) space
List<List<int>> numList = [];
for (int i = 0; i < n; i++) {
List<int> tmp = [];
for (int j = 0; j < n; j++) {
tmp.Add(0);
}
numList.Add(tmp);
}
}
/* Quadratic order (recursive implementation) */
int QuadraticRecur(int n) {
if (n <= 0) return 0;
int[] nums = new int[n];
Console.WriteLine("Recursion n = " + n + ", nums length = " + nums.Length);
return QuadraticRecur(n - 1);
}
/* Driver Code */
TreeNode? BuildTree(int n) {
if (n == 0) return null;
TreeNode root = new(0) {
left = BuildTree(n - 1),
right = BuildTree(n - 1)
};
return root;
}
[Test]
public void Test() {
int n = 5;
// Constant order
Constant(n);
// Linear order
Linear(n);
LinearRecur(n);
// Exponential order
Quadratic(n);
QuadraticRecur(n);
// Exponential order
TreeNode? root = BuildTree(n);
PrintUtil.PrintTree(root);
}
}

View File

@@ -0,0 +1,195 @@
/**
* File: time_complexity.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class time_complexity {
void Algorithm(int n) {
int a = 1; // +0 (technique 1)
a += n; // +0 (technique 1)
// +n (technique 2)
for (int i = 0; i < 5 * n + 1; i++) {
Console.WriteLine(0);
}
// +n*n (technique 3)
for (int i = 0; i < 2 * n; i++) {
for (int j = 0; j < n + 1; j++) {
Console.WriteLine(0);
}
}
}
// Algorithm A time complexity: constant
void AlgorithmA(int n) {
Console.WriteLine(0);
}
// Algorithm B time complexity: linear
void AlgorithmB(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(0);
}
}
// Algorithm C time complexity: constant
void AlgorithmC(int n) {
for (int i = 0; i < 1000000; i++) {
Console.WriteLine(0);
}
}
/* Constant order */
int Constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
/* Linear order */
int Linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
/* Linear order (traversing array) */
int ArrayTraversal(int[] nums) {
int count = 0;
// Number of iterations is proportional to the array length
foreach (int num in nums) {
count++;
}
return count;
}
/* Exponential order */
int Quadratic(int n) {
int count = 0;
// Number of iterations is quadratically related to the data size n
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
/* Quadratic order (bubble sort) */
int BubbleSort(int[] nums) {
int count = 0; // Counter
// Outer loop: unsorted range is [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
count += 3; // Element swap includes 3 unit operations
}
}
}
return count;
}
/* Exponential order (loop implementation) */
int Exponential(int n) {
int count = 0, bas = 1;
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;
}
bas *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
/* Exponential order (recursive implementation) */
int ExpRecur(int n) {
if (n == 1) return 1;
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
}
/* Logarithmic order (loop implementation) */
int Logarithmic(int n) {
int count = 0;
while (n > 1) {
n /= 2;
count++;
}
return count;
}
/* Logarithmic order (recursive implementation) */
int LogRecur(int n) {
if (n <= 1) return 0;
return LogRecur(n / 2) + 1;
}
/* Linearithmic order */
int LinearLogRecur(int n) {
if (n <= 1) return 1;
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
/* Factorial order (recursive implementation) */
int FactorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// Split from 1 into n
for (int i = 0; i < n; i++) {
count += FactorialRecur(n - 1);
}
return count;
}
[Test]
public void Test() {
// You can modify n to run and observe the trend of the number of operations for various complexities
int n = 8;
Console.WriteLine("Input data size n = " + n);
int count = Constant(n);
Console.WriteLine("Constant order operation count = " + count);
count = Linear(n);
Console.WriteLine("Linear order operation count = " + count);
count = ArrayTraversal(new int[n]);
Console.WriteLine("Linear order (array traversal) operation count = " + count);
count = Quadratic(n);
Console.WriteLine("Quadratic order operation count = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = BubbleSort(nums);
Console.WriteLine("Quadratic order (bubble sort) operation count = " + count);
count = Exponential(n);
Console.WriteLine("Exponential order (loop implementation) operation count = " + count);
count = ExpRecur(n);
Console.WriteLine("Exponential order (recursive implementation) operation count = " + count);
count = Logarithmic(n);
Console.WriteLine("Logarithmic order (loop implementation) operation count = " + count);
count = LogRecur(n);
Console.WriteLine("Logarithmic order (recursive implementation) operation count = " + count);
count = LinearLogRecur(n);
Console.WriteLine("Linearithmic order (recursive implementation) operation count = " + count);
count = FactorialRecur(n);
Console.WriteLine("Factorial order (recursive implementation) operation count = " + count);
}
}

View File

@@ -0,0 +1,49 @@
/**
* File: worst_best_time_complexity.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity {
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
int[] RandomNumbers(int n) {
int[] nums = new int[n];
// Generate array nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// Randomly shuffle array elements
for (int i = 0; i < nums.Length; i++) {
int index = new Random().Next(i, nums.Length);
(nums[i], nums[index]) = (nums[index], nums[i]);
}
return nums;
}
/* Find the index of number 1 in array nums */
int FindOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) {
// 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 i;
}
return -1;
}
/* Driver Code */
[Test]
public void Test() {
for (int i = 0; i < 10; i++) {
int n = 100;
int[] nums = RandomNumbers(n);
int index = FindOne(nums);
Console.WriteLine("\nArray [ 1, 2, ..., n ] after shuffling = " + string.Join(",", nums));
Console.WriteLine("Index of number 1 is " + index);
}
}
}