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,75 @@
/**
* File: iteration.swift
* Created Time: 2023-09-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* for loop */
func forLoop(n: Int) -> Int {
var res = 0
// Sum 1, 2, ..., n-1, n
for i in 1 ... n {
res += i
}
return res
}
/* while loop */
func 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 += 1 // Update condition variable
}
return res
}
/* while loop (two updates) */
func 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 += 1
i *= 2
}
return res
}
/* Nested for loop */
func nestedForLoop(n: Int) -> String {
var res = ""
// 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
}
@main
enum Iteration {
/* Driver Code */
static func main() {
let n = 5
var res = 0
res = forLoop(n: n)
print("\nFor loop sum result res = \(res)")
res = whileLoop(n: n)
print("\nWhile loop sum result res = \(res)")
res = whileLoopII(n: n)
print("\nWhile loop (two updates) sum result res = \(res)")
let resStr = nestedForLoop(n: n)
print("\nNested for loop traversal result \(resStr)")
}
}
@@ -0,0 +1,79 @@
/**
* File: recursion.swift
* Created Time: 2023-09-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Recursion */
func recur(n: Int) -> Int {
// Termination condition
if n == 1 {
return 1
}
// Recurse: recursive call
let res = recur(n: n - 1)
// Return: return result
return n + res
}
/* Simulate recursion using iteration */
func forLoopRecur(n: Int) -> Int {
// Use an explicit stack to simulate the system call stack
var stack: [Int] = []
var res = 0
// Recurse: recursive call
for i in (1 ... n).reversed() {
// Simulate "recurse" with "push"
stack.append(i)
}
// Return: return result
while !stack.isEmpty {
// Simulate "return" with "pop"
res += stack.removeLast()
}
// res = 1+2+3+...+n
return res
}
/* Tail recursion */
func tailRecur(n: Int, res: Int) -> Int {
// Termination condition
if n == 0 {
return res
}
// Tail recursive call
return tailRecur(n: n - 1, res: res + n)
}
/* Fibonacci sequence: recursion */
func 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)
let res = fib(n: n - 1) + fib(n: n - 2)
// Return result f(n)
return res
}
@main
enum Recursion {
/* Driver Code */
static func main() {
let n = 5
var res = 0
res = recursion.recur(n: n)
print("\nRecursion sum result res = \(res)")
res = recursion.forLoopRecur(n: n)
print("\nUsing iteration to simulate recursion sum result res = \(res)")
res = recursion.tailRecur(n: n, res: 0)
print("\nTail recursion sum result res = \(res)")
res = recursion.fib(n: n)
print("\nThe \(n)th Fibonacci number is \(res)")
}
}
@@ -0,0 +1,98 @@
/**
* File: space_complexity.swift
* Created Time: 2023-01-01
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Function */
@discardableResult
func function() -> Int {
// Perform some operations
return 0
}
/* Constant order */
func constant(n: Int) {
// Constants, variables, objects occupy O(1) space
let a = 0
var b = 0
let nums = Array(repeating: 0, count: 10000)
let node = ListNode(x: 0)
// Variables in the loop occupy O(1) space
for _ in 0 ..< n {
let c = 0
}
// Functions in the loop occupy O(1) space
for _ in 0 ..< n {
function()
}
}
/* Linear order */
func linear(n: Int) {
// Array of length n uses O(n) space
let nums = Array(repeating: 0, count: n)
// A list of length n occupies O(n) space
let nodes = (0 ..< n).map { ListNode(x: $0) }
// A hash table of length n occupies O(n) space
let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, "\($0)") })
}
/* Linear order (recursive implementation) */
func linearRecur(n: Int) {
print("Recursion n = \(n)")
if n == 1 {
return
}
linearRecur(n: n - 1)
}
/* Exponential order */
func quadratic(n: Int) {
// 2D list uses O(n^2) space
let numList = Array(repeating: Array(repeating: 0, count: n), count: n)
}
/* Quadratic order (recursive implementation) */
@discardableResult
func quadraticRecur(n: Int) -> Int {
if n <= 0 {
return 0
}
// Array nums has length n, n-1, ..., 2, 1
let nums = Array(repeating: 0, count: n)
print("In recursion n = \(n), nums length = \(nums.count)")
return quadraticRecur(n: n - 1)
}
/* Driver Code */
func buildTree(n: Int) -> TreeNode? {
if n == 0 {
return nil
}
let root = TreeNode(x: 0)
root.left = buildTree(n: n - 1)
root.right = buildTree(n: n - 1)
return root
}
@main
enum SpaceComplexity {
/* Driver Code */
static func main() {
let n = 5
// Constant order
constant(n: n)
// Linear order
linear(n: n)
linearRecur(n: n)
// Exponential order
quadratic(n: n)
quadraticRecur(n: n)
// Exponential order
let root = buildTree(n: n)
PrintUtil.printTree(root: root)
}
}
@@ -0,0 +1,172 @@
/**
* File: time_complexity.swift
* Created Time: 2022-12-26
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Constant order */
func constant(n: Int) -> Int {
var count = 0
let size = 100_000
for _ in 0 ..< size {
count += 1
}
return count
}
/* Linear order */
func linear(n: Int) -> Int {
var count = 0
for _ in 0 ..< n {
count += 1
}
return count
}
/* Linear order (traversing array) */
func arrayTraversal(nums: [Int]) -> Int {
var count = 0
// Number of iterations is proportional to the array length
for _ in nums {
count += 1
}
return count
}
/* Exponential order */
func quadratic(n: Int) -> Int {
var count = 0
// Number of iterations is quadratically related to the data size n
for _ in 0 ..< n {
for _ in 0 ..< n {
count += 1
}
}
return count
}
/* Quadratic order (bubble sort) */
func bubbleSort(nums: inout [Int]) -> Int {
var count = 0 // Counter
// Outer loop: unsorted range is [0, i]
for i in nums.indices.dropFirst().reversed() {
// 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
}
}
}
return count
}
/* Exponential order (loop implementation) */
func 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 _ in 0 ..< n {
for _ in 0 ..< base {
count += 1
}
base *= 2
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
}
/* Exponential order (recursive implementation) */
func expRecur(n: Int) -> Int {
if n == 1 {
return 1
}
return expRecur(n: n - 1) + expRecur(n: n - 1) + 1
}
/* Logarithmic order (loop implementation) */
func logarithmic(n: Int) -> Int {
var count = 0
var n = n
while n > 1 {
n = n / 2
count += 1
}
return count
}
/* Logarithmic order (recursive implementation) */
func logRecur(n: Int) -> Int {
if n <= 1 {
return 0
}
return logRecur(n: n / 2) + 1
}
/* Linearithmic order */
func linearLogRecur(n: Int) -> Int {
if n <= 1 {
return 1
}
var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)
for _ in stride(from: 0, to: n, by: 1) {
count += 1
}
return count
}
/* Factorial order (recursive implementation) */
func factorialRecur(n: Int) -> Int {
if n == 0 {
return 1
}
var count = 0
// Split from 1 into n
for _ in 0 ..< n {
count += factorialRecur(n: n - 1)
}
return count
}
@main
enum TimeComplexity {
/* Driver Code */
static func main() {
// You can modify n to run and observe the trend of the number of operations for various complexities
let n = 8
print("Input data size n = \(n)")
var count = constant(n: n)
print("Constant-time operations count = \(count)")
count = linear(n: n)
print("Linear-time operations count = \(count)")
count = arrayTraversal(nums: Array(repeating: 0, count: n))
print("Linear-time (array traversal) operations count = \(count)")
count = quadratic(n: n)
print("Quadratic-time operations count = \(count)")
var nums = Array(stride(from: n, to: 0, by: -1)) // [n,n-1,...,2,1]
count = bubbleSort(nums: &nums)
print("Quadratic-time (bubble sort) operations count = \(count)")
count = exponential(n: n)
print("Exponential-time (iterative) operations count = \(count)")
count = expRecur(n: n)
print("Exponential-time (recursive) operations count = \(count)")
count = logarithmic(n: n)
print("Logarithmic-time (iterative) operations count = \(count)")
count = logRecur(n: n)
print("Logarithmic-time (recursive) operations count = \(count)")
count = linearLogRecur(n: n)
print("Linearithmic-time (recursive) operations count = \(count)")
count = factorialRecur(n: n)
print("Factorial-time (recursive) operations count = \(count)")
}
}
@@ -0,0 +1,40 @@
/**
* File: worst_best_time_complexity.swift
* Created Time: 2022-12-26
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
func randomNumbers(n: Int) -> [Int] {
// Generate array nums = { 1, 2, 3, ..., n }
var nums = Array(1 ... n)
// Randomly shuffle array elements
nums.shuffle()
return nums
}
/* Find the index of number 1 in array nums */
func findOne(nums: [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
}
@main
enum WorstBestTimeComplexity {
/* Driver Code */
static func main() {
for _ in 0 ..< 10 {
let n = 100
let nums = randomNumbers(n: n)
let index = findOne(nums: nums)
print("Array [ 1, 2, ..., n ] after shuffling = \(nums)")
print("Index of number 1 is \(index)")
}
}
}