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
+62
View File
@@ -0,0 +1,62 @@
/**
* File: heap.swift
* Created Time: 2024-03-17
* Author: nuomi1 (nuomi1@qq.com)
*/
import HeapModule
import utils
func testPush(heap: inout Heap<Int>, val: Int) {
heap.insert(val)
print("\nAfter element \(val) pushes to heap\n")
PrintUtil.printHeap(queue: heap.unordered)
}
func testPop(heap: inout Heap<Int>) {
let val = heap.removeMax()
print("\nAfter heap top element \(val) pops from heap\n")
PrintUtil.printHeap(queue: heap.unordered)
}
@main
enum _Heap {
/* Driver Code */
static func main() {
/* Initialize heap */
// Swift's Heap type supports both max heap and min heap
var heap = Heap<Int>()
/* Element enters heap */
testPush(heap: &heap, val: 1)
testPush(heap: &heap, val: 3)
testPush(heap: &heap, val: 2)
testPush(heap: &heap, val: 5)
testPush(heap: &heap, val: 4)
/* Check if heap is empty */
let peek = heap.max()
print("\nHeap top element is \(peek!)\n")
/* Time complexity is O(n), not O(nlogn) */
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
/* Get heap size */
let size = heap.count
print("\nHeap size is \(size)\n")
/* Check if heap is empty */
let isEmpty = heap.isEmpty
print("\nIs heap empty \(isEmpty)\n")
/* Input list and build heap */
// Time complexity is O(n), not O(nlogn)
let heap2 = Heap([1, 3, 2, 5, 4])
print("\nAfter input list and build heap")
PrintUtil.printHeap(queue: heap2.unordered)
}
}
+163
View File
@@ -0,0 +1,163 @@
/**
* File: my_heap.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Max heap */
class MaxHeap {
private var maxHeap: [Int]
/* Constructor, build heap based on input list */
init(nums: [Int]) {
// Add list elements to heap as is
maxHeap = nums
// Heapify all nodes except leaf nodes
for i in (0 ... parent(i: size() - 1)).reversed() {
siftDown(i: i)
}
}
/* Get index of left child node */
private func left(i: Int) -> Int {
2 * i + 1
}
/* Get index of right child node */
private func right(i: Int) -> Int {
2 * i + 2
}
/* Get index of parent node */
private func parent(i: Int) -> Int {
(i - 1) / 2 // Floor division
}
/* Swap elements */
private func swap(i: Int, j: Int) {
maxHeap.swapAt(i, j)
}
/* Get heap size */
func size() -> Int {
maxHeap.count
}
/* Check if heap is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Access top element */
func peek() -> Int {
maxHeap[0]
}
/* Element enters heap */
func push(val: Int) {
// Add node
maxHeap.append(val)
// Heapify from bottom to top
siftUp(i: size() - 1)
}
/* Starting from node i, heapify from bottom to top */
private func siftUp(i: Int) {
var i = i
while true {
// Get parent node of node i
let p = parent(i: i)
// When "crossing root node" or "node needs no repair", end heapify
if p < 0 || maxHeap[i] <= maxHeap[p] {
break
}
// Swap two nodes
swap(i: i, j: p)
// Loop upward heapify
i = p
}
}
/* Element exits heap */
func pop() -> Int {
// Handle empty case
if isEmpty() {
fatalError("Heap is empty")
}
// Delete node
swap(i: 0, j: size() - 1)
// Remove node
let val = maxHeap.remove(at: size() - 1)
// Return top element
siftDown(i: 0)
// Return heap top element
return val
}
/* Starting from node i, heapify from top to bottom */
private func siftDown(i: Int) {
var i = i
while true {
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
let l = left(i: i)
let r = right(i: i)
var ma = i
if l < size(), maxHeap[l] > maxHeap[ma] {
ma = l
}
if r < size(), maxHeap[r] > maxHeap[ma] {
ma = r
}
// Swap two nodes
if ma == i {
break
}
// Swap two nodes
swap(i: i, j: ma)
// Loop downwards heapification
i = ma
}
}
/* Driver Code */
func print() {
let queue = maxHeap
PrintUtil.printHeap(queue: queue)
}
}
@main
enum MyHeap {
/* Driver Code */
static func main() {
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
let maxHeap = MaxHeap(nums: [9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
print("\nAfter inputting list and building heap")
maxHeap.print()
/* Check if heap is empty */
var peek = maxHeap.peek()
print("\nHeap top element is \(peek)")
/* Element enters heap */
let val = 7
maxHeap.push(val: val)
print("\nAfter element \(val) pushes to heap")
maxHeap.print()
/* Time complexity is O(n), not O(nlogn) */
peek = maxHeap.pop()
print("\nAfter heap top element \(peek) pops from heap")
maxHeap.print()
/* Get heap size */
let size = maxHeap.size()
print("\nHeap size is \(size)")
/* Check if heap is empty */
let isEmpty = maxHeap.isEmpty()
print("\nIs heap empty \(isEmpty)")
}
}
+36
View File
@@ -0,0 +1,36 @@
/**
* File: top_k.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
import HeapModule
import utils
/* Find the largest k elements in array based on heap */
func topKHeap(nums: [Int], k: Int) -> [Int] {
// Initialize min heap and build heap with first k elements
var heap = Heap(nums.prefix(k))
// Starting from the (k+1)th element, maintain heap length as k
for i in nums.indices.dropFirst(k) {
// If current element is greater than top element, top element exits heap, current element enters heap
if nums[i] > heap.min()! {
_ = heap.removeMin()
heap.insert(nums[i])
}
}
return heap.unordered
}
@main
enum TopK {
/* Driver Code */
static func main() {
let nums = [1, 7, 6, 3, 2]
let k = 3
let res = topKHeap(nums: nums, k: k)
print("The largest \(k) elements are")
PrintUtil.printHeap(queue: res)
}
}