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
+2
View File
@@ -0,0 +1,2 @@
add_executable(my_heap_test my_heap_test.c)
add_executable(top_k top_k.c)
+152
View File
@@ -0,0 +1,152 @@
/**
* File: my_heap.c
* Created Time: 2023-01-15
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
#define MAX_SIZE 5000
/* Max heap */
typedef struct {
// size represents the actual number of elements
int size;
// Use pre-allocated array to avoid expansion
int data[MAX_SIZE];
} MaxHeap;
// Function declaration
void siftDown(MaxHeap *maxHeap, int i);
void siftUp(MaxHeap *maxHeap, int i);
int parent(MaxHeap *maxHeap, int i);
/* Constructor, build heap from slice */
MaxHeap *newMaxHeap(int nums[], int size) {
// Push all elements to heap
MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));
maxHeap->size = size;
memcpy(maxHeap->data, nums, size * sizeof(int));
for (int i = parent(maxHeap, size - 1); i >= 0; i--) {
// Heapify all nodes except leaf nodes
siftDown(maxHeap, i);
}
return maxHeap;
}
/* Destructor */
void delMaxHeap(MaxHeap *maxHeap) {
// Free memory
free(maxHeap);
}
/* Get index of left child node */
int left(MaxHeap *maxHeap, int i) {
return 2 * i + 1;
}
/* Get index of right child node */
int right(MaxHeap *maxHeap, int i) {
return 2 * i + 2;
}
/* Get index of parent node */
int parent(MaxHeap *maxHeap, int i) {
return (i - 1) / 2; // Round down
}
/* Swap elements */
void swap(MaxHeap *maxHeap, int i, int j) {
int temp = maxHeap->data[i];
maxHeap->data[i] = maxHeap->data[j];
maxHeap->data[j] = temp;
}
/* Get heap size */
int size(MaxHeap *maxHeap) {
return maxHeap->size;
}
/* Check if heap is empty */
int isEmpty(MaxHeap *maxHeap) {
return maxHeap->size == 0;
}
/* Access top element */
int peek(MaxHeap *maxHeap) {
return maxHeap->data[0];
}
/* Element enters heap */
void push(MaxHeap *maxHeap, int val) {
// By default, should not add this many nodes
if (maxHeap->size == MAX_SIZE) {
printf("heap is full!");
return;
}
// Add node
maxHeap->data[maxHeap->size] = val;
maxHeap->size++;
// Heapify from bottom to top
siftUp(maxHeap, maxHeap->size - 1);
}
/* Element exits heap */
int pop(MaxHeap *maxHeap) {
// Handle empty case
if (isEmpty(maxHeap)) {
printf("heap is empty!");
return INT_MAX;
}
// Delete node
swap(maxHeap, 0, size(maxHeap) - 1);
// Remove node
int val = maxHeap->data[maxHeap->size - 1];
maxHeap->size--;
// Return top element
siftDown(maxHeap, 0);
// Return heap top element
return val;
}
/* Starting from node i, heapify from top to bottom */
void siftDown(MaxHeap *maxHeap, int i) {
while (true) {
// Find node with maximum value among nodes i, l, r, denoted as max
int l = left(maxHeap, i);
int r = right(maxHeap, i);
int max = i;
if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {
max = l;
}
if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {
max = r;
}
// Swap two nodes
if (max == i) {
break;
}
// Swap two nodes
swap(maxHeap, i, max);
// Loop downwards heapification
i = max;
}
}
/* Starting from node i, heapify from bottom to top */
void siftUp(MaxHeap *maxHeap, int i) {
while (true) {
// Get parent node of node i
int p = parent(maxHeap, i);
// When "crossing root node" or "node needs no repair", end heapify
if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {
break;
}
// Swap two nodes
swap(maxHeap, i, p);
// Loop upward heapify
i = p;
}
}
+41
View File
@@ -0,0 +1,41 @@
/**
* File: my_heap_test.c
* Created Time: 2023-01-15
* Author: Reanon (793584285@qq.com)
*/
#include "my_heap.c"
/* Driver Code */
int main() {
/* Initialize heap */
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
int nums[] = {9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
MaxHeap *maxHeap = newMaxHeap(nums, sizeof(nums) / sizeof(int));
printf("After input array and building heap\n");
printHeap(maxHeap->data, maxHeap->size);
/* Check if heap is empty */
printf("\nHeap top element is %d\n", peek(maxHeap));
/* Element enters heap */
push(maxHeap, 7);
printf("\nAfter element 7 pushes to heap\n");
printHeap(maxHeap->data, maxHeap->size);
/* Time complexity is O(n), not O(nlogn) */
int top = pop(maxHeap);
printf("\nAfter heap top element %d exits heap\n", top);
printHeap(maxHeap->data, maxHeap->size);
/* Get heap size */
printf("\nHeap element count is %d\n", size(maxHeap));
/* Check if heap is empty */
printf("\nIs heap empty %d\n", isEmpty(maxHeap));
// Free memory
delMaxHeap(maxHeap);
return 0;
}
+73
View File
@@ -0,0 +1,73 @@
/**
* File: top_k.c
* Created Time: 2023-10-26
* Author: krahets (krahets163.com)
*/
#include "my_heap.c"
/* Element enters heap */
void pushMinHeap(MaxHeap *maxHeap, int val) {
// Negate element
push(maxHeap, -val);
}
/* Element exits heap */
int popMinHeap(MaxHeap *maxHeap) {
// Negate element
return -pop(maxHeap);
}
/* Access top element */
int peekMinHeap(MaxHeap *maxHeap) {
// Negate element
return -peek(maxHeap);
}
/* Extract elements from heap */
int *getMinHeap(MaxHeap *maxHeap) {
// Negate all heap elements and store in res array
int *res = (int *)malloc(maxHeap->size * sizeof(int));
for (int i = 0; i < maxHeap->size; i++) {
res[i] = -maxHeap->data[i];
}
return res;
}
// Function to find k largest elements in array using heap
int *topKHeap(int *nums, int sizeNums, int k) {
// Python's heapq module implements min heap by default
// Note: We negate all heap elements to simulate min heap using max heap
int *empty = (int *)malloc(0);
MaxHeap *maxHeap = newMaxHeap(empty, 0);
// Enter the first k elements of array into heap
for (int i = 0; i < k; i++) {
pushMinHeap(maxHeap, nums[i]);
}
// Starting from the (k+1)th element, maintain heap length as k
for (int i = k; i < sizeNums; i++) {
// If current element is greater than top element, top element exits heap, current element enters heap
if (nums[i] > peekMinHeap(maxHeap)) {
popMinHeap(maxHeap);
pushMinHeap(maxHeap, nums[i]);
}
}
int *res = getMinHeap(maxHeap);
// Free memory
delMaxHeap(maxHeap);
return res;
}
/* Driver Code */
int main() {
int nums[] = {1, 7, 6, 3, 2};
int k = 3;
int sizeNums = sizeof(nums) / sizeof(nums[0]);
int *res = topKHeap(nums, sizeNums, k);
printf("The largest %d elements are: ", k);
printArray(res, k);
free(res);
return 0;
}