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,102 @@
/**
* File: array.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_array_and_linkedlist
import java.util.concurrent.ThreadLocalRandom
/* Random access to element */
fun randomAccess(nums: IntArray): Int {
// Randomly select a number in interval [0, nums.size)
val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)
// Retrieve and return the random element
val randomNum = nums[randomIndex]
return randomNum
}
/* Extend array length */
fun extend(nums: IntArray, enlarge: Int): IntArray {
// Initialize an array with extended length
val res = IntArray(nums.size + enlarge)
// Copy all elements from the original array to the new array
for (i in nums.indices) {
res[i] = nums[i]
}
// Return the extended new array
return res
}
/* Insert element num at index index in the array */
fun insert(nums: IntArray, num: Int, index: Int) {
// Move all elements at and after index index backward by one position
for (i in nums.size - 1 downTo index + 1) {
nums[i] = nums[i - 1]
}
// Assign num to the element at index index
nums[index] = num
}
/* Remove the element at index index */
fun remove(nums: IntArray, index: Int) {
// Move all elements after index index forward by one position
for (i in index..<nums.size - 1) {
nums[i] = nums[i + 1]
}
}
/* Traverse array */
fun traverse(nums: IntArray) {
var count = 0
// Traverse array by index
for (i in nums.indices) {
count += nums[i]
}
// Direct traversal of array elements
for (j in nums) {
count += j
}
}
/* Find the specified element in the array */
fun find(nums: IntArray, target: Int): Int {
for (i in nums.indices) {
if (nums[i] == target)
return i
}
return -1
}
/* Driver Code */
fun main() {
/* Initialize array */
val arr = IntArray(5)
println("Array arr = ${arr.contentToString()}")
var nums = intArrayOf(1, 3, 2, 5, 4)
println("Array nums = ${nums.contentToString()}")
/* Insert element */
val randomNum: Int = randomAccess(nums)
println("Get random element $randomNum from nums")
/* Traverse array */
nums = extend(nums, 3)
println("Extend array length to 8, get nums = ${nums.contentToString()}")
/* Insert element */
insert(nums, 6, 3)
println("Insert number 6 at index 3, get nums = ${nums.contentToString()}")
/* Remove element */
remove(nums, 2)
println("Delete element at index 2, get nums = ${nums.contentToString()}")
/* Traverse array */
traverse(nums)
/* Find element */
val index: Int = find(nums, 3)
println("Find element 3 in nums, index = $index")
}
@@ -0,0 +1,88 @@
/**
* File: linked_list.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_array_and_linkedlist
import utils.ListNode
import utils.printLinkedList
/* Insert node P after node n0 in the linked list */
fun insert(n0: ListNode?, p: ListNode?) {
val n1 = n0?.next
p?.next = n1
n0?.next = p
}
/* Remove the first node after node n0 in the linked list */
fun remove(n0: ListNode?) {
if (n0?.next == null)
return
// n0 -> P -> n1
val p = n0.next
val n1 = p?.next
n0.next = n1
}
/* Access the node at index index in the linked list */
fun access(head: ListNode?, index: Int): ListNode? {
var h = head
for (i in 0..<index) {
if (h == null)
return null
h = h.next
}
return h
}
/* Find the first node with value target in the linked list */
fun find(head: ListNode?, target: Int): Int {
var index = 0
var h = head
while (h != null) {
if (h._val == target)
return index
h = h.next
index++
}
return -1
}
/* Driver Code */
fun main() {
/* Initialize linked list */
// Initialize each node
val n0 = ListNode(1)
val n1 = ListNode(3)
val n2 = ListNode(2)
val n3 = ListNode(5)
val n4 = ListNode(4)
// Build references between nodes
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
println("Initialized linked list is")
printLinkedList(n0)
/* Insert node */
insert(n0, ListNode(0))
println("Linked list after inserting node is")
printLinkedList(n0)
/* Remove node */
remove(n0)
println("Linked list after removing node is")
printLinkedList(n0)
/* Access node */
val node = access(n0, 3)!!
println("Value of node at index 3 in linked list = ${node._val}")
/* Search node */
val index = find(n0, 2)
println("Index of node with value 2 in linked list = $index")
}
@@ -0,0 +1,63 @@
/**
* File: list.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_array_and_linkedlist
/* Driver Code */
fun main() {
/* Initialize list */
// Mutable collection
val nums = mutableListOf(1, 3, 2, 5, 4)
println("List nums = $nums")
/* Update element */
val num = nums[1]
println("Access element at index 1, get num = $num")
/* Add elements at the end */
nums[1] = 0
println("Update element at index 1 to 0, get nums = $nums")
/* Remove element */
nums.clear()
println("After clearing list, nums = $nums")
/* Direct traversal of list elements */
nums.add(1)
nums.add(3)
nums.add(2)
nums.add(5)
nums.add(4)
println("After adding elements, nums = $nums")
/* Sort list */
nums.add(3, 6)
println("Insert number 6 at index 3, get nums = $nums")
/* Remove element */
nums.removeAt(3)
println("Delete element at index 3, get nums = $nums")
/* Traverse list by index */
var count = 0
for (i in nums.indices) {
count += nums[i]
}
/* Directly traverse list elements */
for (j in nums) {
count += j
}
/* Concatenate two lists */
val nums1 = mutableListOf(6, 8, 7, 10, 9)
nums.addAll(nums1)
println("After concatenating list nums1 to nums, get nums = $nums")
/* Sort list */
nums.sort()
println("After sorting list, nums = $nums")
}
@@ -0,0 +1,139 @@
/**
* File: my_list.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_array_and_linkedlist
/* List class */
class MyList {
private var arr: IntArray = intArrayOf() // Array (stores list elements)
private var capacity: Int = 10 // List capacity
private var size: Int = 0 // List length (current number of elements)
private var extendRatio: Int = 2 // Multiple by which the list capacity is extended each time
/* Constructor */
init {
arr = IntArray(capacity)
}
/* Get list length (current number of elements) */
fun size(): Int {
return size
}
/* Get list capacity */
fun capacity(): Int {
return capacity
}
/* Update element */
fun get(index: Int): Int {
// If the index is out of bounds, throw an exception, as below
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("Index out of bounds")
return arr[index]
}
/* Add elements at the end */
fun set(index: Int, num: Int) {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("Index out of bounds")
arr[index] = num
}
/* Direct traversal of list elements */
fun add(num: Int) {
// When the number of elements exceeds capacity, trigger the extension mechanism
if (size == capacity())
extendCapacity()
arr[size] = num
// Update the number of elements
size++
}
/* Sort list */
fun insert(index: Int, num: Int) {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("Index out of bounds")
// When the number of elements exceeds capacity, trigger the extension mechanism
if (size == capacity())
extendCapacity()
// Move all elements after index index forward by one position
for (j in size - 1 downTo index)
arr[j + 1] = arr[j]
arr[index] = num
// Update the number of elements
size++
}
/* Remove element */
fun remove(index: Int): Int {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("Index out of bounds")
val num = arr[index]
// Move all elements after index forward by one position
for (j in index..<size - 1)
arr[j] = arr[j + 1]
// Update the number of elements
size--
// Return the removed element
return num
}
/* Driver Code */
fun extendCapacity() {
// Create a new array with length extendRatio times the original array and copy the original array to the new array
arr = arr.copyOf(capacity() * extendRatio)
// Add elements at the end
capacity = arr.size
}
/* Convert list to array */
fun toArray(): IntArray {
val size = size()
// Elements enqueue
val arr = IntArray(size)
for (i in 0..<size) {
arr[i] = get(i)
}
return arr
}
}
/* Driver Code */
fun main() {
/* Initialize list */
val nums = MyList()
/* Direct traversal of list elements */
nums.add(1)
nums.add(3)
nums.add(2)
nums.add(5)
nums.add(4)
println("List nums = ${nums.toArray().contentToString()}, capacity = ${nums.capacity()}, length = ${nums.size()}")
/* Sort list */
nums.insert(3, 6)
println("Insert number 6 at index 3, get nums = ${nums.toArray().contentToString()}")
/* Remove element */
nums.remove(3)
println("Delete element at index 3, get nums = ${nums.toArray().contentToString()}")
/* Update element */
val num = nums.get(1)
println("Access element at index 1, get num = $num")
/* Add elements at the end */
nums.set(1, 0)
println("Update element at index 1 to 0, get nums = ${nums.toArray().contentToString()}")
/* Test capacity expansion mechanism */
for (i in 0..9) {
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
nums.add(i)
}
println("After expansion, list nums = ${nums.toArray().contentToString()}, capacity = ${nums.capacity()}, length = ${nums.size()}")
}