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.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_computational_complexity.iteration
/* for loop */
fun forLoop(n: Int): Int {
var res = 0
// Sum 1, 2, ..., n-1, n
for (i in 1..n) {
res += i
}
return res
}
/* while loop */
fun whileLoop(n: Int): Int {
var res = 0
var i = 1 // Initialize condition variable
// Sum 1, 2, ..., n-1, n
while (i <= n) {
res += i
i++ // Update condition variable
}
return res
}
/* while loop (two updates) */
fun whileLoopII(n: Int): Int {
var res = 0
var i = 1 // Initialize condition variable
// Sum 1, 4, 10, ...
while (i <= n) {
res += i
// Update condition variable
i++
i *= 2
}
return res
}
/* Nested for loop */
fun nestedForLoop(n: Int): String {
val res = StringBuilder()
// 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.append(" ($i, $j), ")
}
}
return res.toString()
}
/* Driver Code */
fun main() {
val n = 5
var res: Int
res = forLoop(n)
println("\nFor loop sum result res = $res")
res = whileLoop(n)
println("\nWhile loop sum result res = $res")
res = whileLoopII(n)
println("\nWhile loop (two updates) sum result res = $res")
val resStr = nestedForLoop(n)
println("\nNested for loop traversal result $resStr")
}
@@ -0,0 +1,78 @@
/**
* File: recursion.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_computational_complexity.recursion
import java.util.*
/* Recursion */
fun recur(n: Int): Int {
// Termination condition
if (n == 1)
return 1
// Descend: recursive call
val res = recur(n - 1)
// Return: return result
return n + res
}
/* Simulate recursion using iteration */
fun forLoopRecur(n: Int): Int {
// Use an explicit stack to simulate the system call stack
val stack = Stack<Int>()
var res = 0
// Descend: recursive call
for (i in n downTo 0) {
// Simulate "recurse" with "push"
stack.push(i)
}
// Return: return result
while (stack.isNotEmpty()) {
// Simulate "return" with "pop"
res += stack.pop()
}
// res = 1+2+3+...+n
return res
}
/* Tail recursion */
tailrec fun tailRecur(n: Int, res: Int): Int {
// Add tailrec keyword to enable tail recursion optimization
// Termination condition
if (n == 0)
return res
// Tail recursive call
return tailRecur(n - 1, res + n)
}
/* Fibonacci sequence: recursion */
fun fib(n: Int): Int {
// 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)
val res = fib(n - 1) + fib(n - 2)
// Return result f(n)
return res
}
/* Driver Code */
fun main() {
val n = 5
var res: Int
res = recur(n)
println("\nRecursion sum result res = $res")
res = forLoopRecur(n)
println("\nUsing iteration to simulate recursion sum result res = $res")
res = tailRecur(n, 0)
println("\nTail recursion sum result res = $res")
res = fib(n)
println("\nThe ${n}th Fibonacci number is $res")
}
@@ -0,0 +1,109 @@
/**
* File: space_complexity.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_computational_complexity.space_complexity
import utils.ListNode
import utils.TreeNode
import utils.printTree
/* Function */
fun function(): Int {
// Perform some operations
return 0
}
/* Constant order */
fun constant(n: Int) {
// Constants, variables, objects occupy O(1) space
val a = 0
var b = 0
val nums = Array(10000) { 0 }
val node = ListNode(0)
// Variables in the loop occupy O(1) space
for (i in 0..<n) {
val c = 0
}
// Functions in the loop occupy O(1) space
for (i in 0..<n) {
function()
}
}
/* Linear order */
fun linear(n: Int) {
// Array of length n uses O(n) space
val nums = Array(n) { 0 }
// A list of length n occupies O(n) space
val nodes = mutableListOf<ListNode>()
for (i in 0..<n) {
nodes.add(ListNode(i))
}
// A hash table of length n occupies O(n) space
val map = mutableMapOf<Int, String>()
for (i in 0..<n) {
map[i] = i.toString()
}
}
/* Linear order (recursive implementation) */
fun linearRecur(n: Int) {
println("Recursion n = $n")
if (n == 1)
return
linearRecur(n - 1)
}
/* Exponential order */
fun quadratic(n: Int) {
// Matrix uses O(n^2) space
val numMatrix = arrayOfNulls<Array<Int>?>(n)
// 2D list uses O(n^2) space
val numList = mutableListOf<MutableList<Int>>()
for (i in 0..<n) {
val tmp = mutableListOf<Int>()
for (j in 0..<n) {
tmp.add(0)
}
numList.add(tmp)
}
}
/* Quadratic order (recursive implementation) */
tailrec fun quadraticRecur(n: Int): Int {
if (n <= 0)
return 0
// Array nums has length n, n-1, ..., 2, 1
val nums = Array(n) { 0 }
println("In recursion n = $n, nums length = ${nums.size}")
return quadraticRecur(n - 1)
}
/* Driver Code */
fun buildTree(n: Int): TreeNode? {
if (n == 0)
return null
val root = TreeNode(0)
root.left = buildTree(n - 1)
root.right = buildTree(n - 1)
return root
}
/* Driver Code */
fun main() {
val n = 5
// Constant order
constant(n)
// Linear order
linear(n)
linearRecur(n)
// Exponential order
quadratic(n)
quadraticRecur(n)
// Exponential order
val root = buildTree(n)
printTree(root)
}
@@ -0,0 +1,168 @@
/**
* File: time_complexity.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_computational_complexity.time_complexity
/* Constant order */
fun constant(n: Int): Int {
var count = 0
val size = 100000
for (i in 0..<size)
count++
return count
}
/* Linear order */
fun linear(n: Int): Int {
var count = 0
for (i in 0..<n)
count++
return count
}
/* Linear order (traversing array) */
fun arrayTraversal(nums: IntArray): Int {
var count = 0
// Number of iterations is proportional to the array length
for (num in nums) {
count++
}
return count
}
/* Exponential order */
fun quadratic(n: Int): Int {
var count = 0
// Number of iterations is quadratically related to the data size n
for (i in 0..<n) {
for (j in 0..<n) {
count++
}
}
return count
}
/* Quadratic order (bubble sort) */
fun bubbleSort(nums: IntArray): Int {
var count = 0 // Counter
// Outer loop: unsorted range is [0, i]
for (i in nums.size - 1 downTo 1) {
// 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]
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
count += 3 // Element swap includes 3 unit operations
}
}
}
return count
}
/* Exponential order (loop implementation) */
fun exponential(n: Int): Int {
var count = 0
var base = 1
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
for (i in 0..<n) {
for (j in 0..<base) {
count++
}
base *= 2
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
}
/* Exponential order (recursive implementation) */
fun expRecur(n: Int): Int {
if (n == 1) {
return 1
}
return expRecur(n - 1) + expRecur(n - 1) + 1
}
/* Logarithmic order (loop implementation) */
fun logarithmic(n: Int): Int {
var n1 = n
var count = 0
while (n1 > 1) {
n1 /= 2
count++
}
return count
}
/* Logarithmic order (recursive implementation) */
fun logRecur(n: Int): Int {
if (n <= 1)
return 0
return logRecur(n / 2) + 1
}
/* Linearithmic order */
fun linearLogRecur(n: Int): Int {
if (n <= 1)
return 1
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (i in 0..<n) {
count++
}
return count
}
/* Factorial order (recursive implementation) */
fun factorialRecur(n: Int): Int {
if (n == 0)
return 1
var count = 0
// Split from 1 into n
for (i in 0..<n) {
count += factorialRecur(n - 1)
}
return count
}
/* Driver Code */
fun main() {
// You can modify n to run and observe the trend of the number of operations for various complexities
val n = 8
println("Input data size n = $n")
var count = constant(n)
println("Constant-time operations count = $count")
count = linear(n)
println("Linear-time operations count = $count")
count = arrayTraversal(IntArray(n))
println("Linear-time (array traversal) operations count = $count")
count = quadratic(n)
println("Quadratic-time operations count = $count")
val nums = IntArray(n)
for (i in 0..<n)
nums[i] = n - i // [n,n-1,...,2,1]
count = bubbleSort(nums)
println("Quadratic-time (bubble sort) operations count = $count")
count = exponential(n)
println("Exponential-time (iterative) operations count = $count")
count = expRecur(n)
println("Exponential-time (recursive) operations count = $count")
count = logarithmic(n)
println("Logarithmic-time (iterative) operations count = $count")
count = logRecur(n)
println("Logarithmic-time (recursive) operations count = $count")
count = linearLogRecur(n)
println("Linearithmic-time (recursive) operations count = $count")
count = factorialRecur(n)
println("Factorial-time (recursive) operations count = $count")
}
@@ -0,0 +1,45 @@
/**
* File: worst_best_time_complexity.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_computational_complexity.worst_best_time_complexity
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
fun randomNumbers(n: Int): Array<Int?> {
val nums = IntArray(n)
// Generate array nums = { 1, 2, 3, ..., n }
for (i in 0..<n) {
nums[i] = i + 1
}
// Randomly shuffle array elements
nums.shuffle()
val res = arrayOfNulls<Int>(n)
for (i in 0..<n) {
res[i] = nums[i]
}
return res
}
/* Find the index of number 1 in array nums */
fun findOne(nums: Array<Int?>): Int {
for (i in nums.indices) {
// 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 */
fun main() {
for (i in 0..9) {
val n = 100
val nums = randomNumbers(n)
val index = findOne(nums)
println("\nArray [ 1, 2, ..., n ] after shuffling = ${nums.contentToString()}")
println("Index of number 1 is $index")
}
}