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
+45
View File
@@ -0,0 +1,45 @@
// File: heap.go
// Created Time: 2023-01-12
// Author: Reanon (793584285@qq.com)
package chapter_heap
// In Go, integer max heap can be built by implementing heap.Interface
// Implementing heap.Interface requires also implementing sort.Interface
type intHeap []any
// Push function of heap.Interface, implements pushing element to heap
func (h *intHeap) Push(x any) {
// Push and Pop use pointer receiver as parameter
// Because they not only adjust the slice content, but also modify the slice length.
*h = append(*h, x.(int))
}
// Pop function of heap.Interface, implements popping heap top element
func (h *intHeap) Pop() any {
// Element to be popped is stored at the end
last := (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
return last
}
// Len function of sort.Interface
func (h *intHeap) Len() int {
return len(*h)
}
// Less function of sort.Interface
func (h *intHeap) Less(i, j int) bool {
// If implementing min heap, need to change to less than sign
return (*h)[i].(int) > (*h)[j].(int)
}
// Swap function of sort.Interface
func (h *intHeap) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
// Top gets heap top element
func (h *intHeap) Top() any {
return (*h)[0]
}
+101
View File
@@ -0,0 +1,101 @@
// File: heap_test.go
// Created Time: 2023-01-12
// Author: Reanon (793584285@qq.com)
package chapter_heap
import (
"container/heap"
"fmt"
"strconv"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func testPush(h *intHeap, val int) {
// Call heap.Interface function to add element
heap.Push(h, val)
fmt.Printf("\nAfter element %d pushes to heap \n", val)
PrintHeap(*h)
}
func testPop(h *intHeap) {
// Call heap.Interface function to remove element
val := heap.Pop(h)
fmt.Printf("\nAfter heap top element %d pops from heap \n", val)
PrintHeap(*h)
}
func TestHeap(t *testing.T) {
/* Initialize heap */
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
maxHeap := &intHeap{}
heap.Init(maxHeap)
/* Element enters heap */
testPush(maxHeap, 1)
testPush(maxHeap, 3)
testPush(maxHeap, 2)
testPush(maxHeap, 5)
testPush(maxHeap, 4)
/* Check if heap is empty */
top := maxHeap.Top()
fmt.Printf("Heap top element is %d\n", top)
/* Time complexity is O(n), not O(nlogn) */
testPop(maxHeap)
testPop(maxHeap)
testPop(maxHeap)
testPop(maxHeap)
testPop(maxHeap)
/* Get heap size */
size := len(*maxHeap)
fmt.Printf("Heap size is %d\n", size)
/* Check if heap is empty */
isEmpty := len(*maxHeap) == 0
fmt.Printf("Is heap empty %t\n", isEmpty)
}
func TestMyHeap(t *testing.T) {
/* Initialize heap */
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
maxHeap := newMaxHeap([]any{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2})
fmt.Printf("After input array and building heap\n")
maxHeap.print()
/* Check if heap is empty */
peek := maxHeap.peek()
fmt.Printf("\nHeap top element is %d\n", peek)
/* Element enters heap */
val := 7
maxHeap.push(val)
fmt.Printf("\nAfter element %d enters heap\n", val)
maxHeap.print()
/* Time complexity is O(n), not O(nlogn) */
peek = maxHeap.pop()
fmt.Printf("\nAfter heap top element %d exits heap\n", peek)
maxHeap.print()
/* Get heap size */
size := maxHeap.size()
fmt.Printf("\nHeap element count is %d\n", size)
/* Check if heap is empty */
isEmpty := maxHeap.isEmpty()
fmt.Printf("\nIs heap empty %t\n", isEmpty)
}
func TestTopKHeap(t *testing.T) {
/* Initialize heap */
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
nums := []int{1, 7, 6, 3, 2}
k := 3
res := topKHeap(nums, k)
fmt.Printf("The largest " + strconv.Itoa(k) + " elements are")
PrintHeap(*res)
}
+140
View File
@@ -0,0 +1,140 @@
// File: my_heap.go
// Created Time: 2023-01-12
// Author: Reanon (793584285@qq.com)
package chapter_heap
import (
"fmt"
. "github.com/krahets/hello-algo/pkg"
)
type maxHeap struct {
// Use slice instead of array to avoid expansion issues
data []any
}
/* Constructor, build empty heap */
func newHeap() *maxHeap {
return &maxHeap{
data: make([]any, 0),
}
}
/* Constructor, build heap from slice */
func newMaxHeap(nums []any) *maxHeap {
// Add list elements to heap as is
h := &maxHeap{data: nums}
for i := h.parent(len(h.data) - 1); i >= 0; i-- {
// Heapify all nodes except leaf nodes
h.siftDown(i)
}
return h
}
/* Get index of left child node */
func (h *maxHeap) left(i int) int {
return 2*i + 1
}
/* Get index of right child node */
func (h *maxHeap) right(i int) int {
return 2*i + 2
}
/* Get index of parent node */
func (h *maxHeap) parent(i int) int {
// Floor division
return (i - 1) / 2
}
/* Swap elements */
func (h *maxHeap) swap(i, j int) {
h.data[i], h.data[j] = h.data[j], h.data[i]
}
/* Get heap size */
func (h *maxHeap) size() int {
return len(h.data)
}
/* Check if heap is empty */
func (h *maxHeap) isEmpty() bool {
return len(h.data) == 0
}
/* Access top element */
func (h *maxHeap) peek() any {
return h.data[0]
}
/* Element enters heap */
func (h *maxHeap) push(val any) {
// Add node
h.data = append(h.data, val)
// Heapify from bottom to top
h.siftUp(len(h.data) - 1)
}
/* Starting from node i, heapify from bottom to top */
func (h *maxHeap) siftUp(i int) {
for true {
// Get parent node of node i
p := h.parent(i)
// When "crossing root node" or "node needs no repair", end heapify
if p < 0 || h.data[i].(int) <= h.data[p].(int) {
break
}
// Swap two nodes
h.swap(i, p)
// Loop upward heapify
i = p
}
}
/* Element exits heap */
func (h *maxHeap) pop() any {
// Handle empty case
if h.isEmpty() {
fmt.Println("error")
return nil
}
// Delete node
h.swap(0, h.size()-1)
// Remove node
val := h.data[len(h.data)-1]
h.data = h.data[:len(h.data)-1]
// Return top element
h.siftDown(0)
// Return heap top element
return val
}
/* Starting from node i, heapify from top to bottom */
func (h *maxHeap) siftDown(i int) {
for true {
// Find node with maximum value among nodes i, l, r, denoted as max
l, r, max := h.left(i), h.right(i), i
if l < h.size() && h.data[l].(int) > h.data[max].(int) {
max = l
}
if r < h.size() && h.data[r].(int) > h.data[max].(int) {
max = r
}
// Swap two nodes
if max == i {
break
}
// Swap two nodes
h.swap(i, max)
// Loop downwards heapification
i = max
}
}
/* Driver Code */
func (h *maxHeap) print() {
PrintHeap(h.data)
}
+51
View File
@@ -0,0 +1,51 @@
// File: top_k.go
// Created Time: 2023-06-24
// Author: Reanon (793584285@qq.com)
package chapter_heap
import "container/heap"
type minHeap []any
func (h *minHeap) Len() int { return len(*h) }
func (h *minHeap) Less(i, j int) bool { return (*h)[i].(int) < (*h)[j].(int) }
func (h *minHeap) Swap(i, j int) { (*h)[i], (*h)[j] = (*h)[j], (*h)[i] }
// Push method of heap.Interface, implements pushing element to heap
func (h *minHeap) Push(x any) {
*h = append(*h, x.(int))
}
// Pop method of heap.Interface, implements popping heap top element
func (h *minHeap) Pop() any {
// Element to be popped is stored at the end
last := (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
return last
}
// Top gets heap top element
func (h *minHeap) Top() any {
return (*h)[0]
}
/* Find the largest k elements in array based on heap */
func topKHeap(nums []int, k int) *minHeap {
// Python's heapq module implements min heap by default
h := &minHeap{}
heap.Init(h)
// Enter the first k elements of array into heap
for i := 0; i < k; i++ {
heap.Push(h, nums[i])
}
// Starting from the (k+1)th element, maintain heap length as k
for i := k; i < len(nums); i++ {
// If current element is greater than top element, top element exits heap, current element enters heap
if nums[i] > h.Top().(int) {
heap.Pop(h)
heap.Push(h, nums[i])
}
}
return h
}