mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-20 02:26:11 +00:00
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:
@@ -0,0 +1,3 @@
|
||||
add_executable(array array.c)
|
||||
add_executable(linked_list linked_list.c)
|
||||
add_executable(my_list my_list.c)
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* File: array.c
|
||||
* Created Time: 2022-12-20
|
||||
* Author: MolDuM (moldum@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* Random access to element */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// Randomly select a number from interval [0, size)
|
||||
int randomIndex = rand() % size;
|
||||
// Retrieve and return the random element
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
int *extend(int *nums, int size, int enlarge) {
|
||||
// Initialize an array with extended length
|
||||
int *res = (int *)malloc(sizeof(int) * (size + enlarge));
|
||||
// Copy all elements from the original array to the new array
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// Initialize expanded space
|
||||
for (int i = size; i < size + enlarge; i++) {
|
||||
res[i] = 0;
|
||||
}
|
||||
// Return the extended new array
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Insert element num at index index in the array */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// Move all elements at and after index index backward by one position
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// Assign num to the element at index index
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* Remove the element at index index */
|
||||
// Note: stdio.h occupies the remove keyword
|
||||
void removeItem(int *nums, int size, int index) {
|
||||
// Move all elements after index index forward by one position
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* Traverse array */
|
||||
void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// Traverse array by index
|
||||
for (int i = 0; i < size; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the specified element in the array */
|
||||
int find(int *nums, int size, int target) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize array */
|
||||
int size = 5;
|
||||
int arr[5];
|
||||
printf("Array arr = ");
|
||||
printArray(arr, size);
|
||||
|
||||
int nums[] = {1, 3, 2, 5, 4};
|
||||
printf("Array nums = ");
|
||||
printArray(nums, size);
|
||||
|
||||
/* Insert element */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
printf("Get random element %d from nums", randomNum);
|
||||
|
||||
/* Traverse array */
|
||||
int enlarge = 3;
|
||||
int *res = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
printf("Extend array length to 8, resulting in nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
/* Insert element */
|
||||
insert(res, size, 6, 3);
|
||||
printf("Insert number 6 at index 3, resulting in nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
/* Remove element */
|
||||
removeItem(res, size, 2);
|
||||
printf("Remove element at index 2, resulting in nums = ");
|
||||
printArray(res, size);
|
||||
|
||||
/* Traverse array */
|
||||
traverse(res, size);
|
||||
|
||||
/* Find element */
|
||||
int index = find(res, size, 3);
|
||||
printf("Find element 3 in res, index = %d\n", index);
|
||||
|
||||
/* Free memory */
|
||||
free(res);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* File: linked_list.c
|
||||
* Created Time: 2023-01-12
|
||||
* Author: Zero (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* Insert node P after node n0 in the linked list */
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
|
||||
/* Remove the first node after node n0 in the linked list */
|
||||
// Note: stdio.h occupies the remove keyword
|
||||
void removeItem(ListNode *n0) {
|
||||
if (!n0->next)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// Free memory
|
||||
free(P);
|
||||
}
|
||||
|
||||
/* Access the node at index index in the linked list */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == NULL)
|
||||
return NULL;
|
||||
head = head->next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* Find the first node with value target in the linked list */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize linked list */
|
||||
// Initialize each node
|
||||
ListNode *n0 = newListNode(1);
|
||||
ListNode *n1 = newListNode(3);
|
||||
ListNode *n2 = newListNode(2);
|
||||
ListNode *n3 = newListNode(5);
|
||||
ListNode *n4 = newListNode(4);
|
||||
// Build references between nodes
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
printf("Initialized linked list is\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Insert node */
|
||||
insert(n0, newListNode(0));
|
||||
printf("Linked list after node insertion is\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Remove node */
|
||||
removeItem(n0);
|
||||
printf("Linked list after node deletion is\r\n");
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Access node */
|
||||
ListNode *node = access(n0, 3);
|
||||
printf("Value of node at index 3 in linked list = %d\r\n", node->val);
|
||||
|
||||
/* Search node */
|
||||
int index = find(n0, 2);
|
||||
printf("Index of node with value 2 in linked list = %d\r\n", index);
|
||||
|
||||
// Free memory
|
||||
freeMemoryLinkedList(n0);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* File: my_list.c
|
||||
* Created Time: 2023-01-12
|
||||
* Author: Zero (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* List class */
|
||||
typedef struct {
|
||||
int *arr; // Array (stores list elements)
|
||||
int capacity; // List capacity
|
||||
int size; // List size
|
||||
int extendRatio; // List expansion multiplier
|
||||
} MyList;
|
||||
|
||||
void extendCapacity(MyList *nums);
|
||||
|
||||
/* Constructor */
|
||||
MyList *newMyList() {
|
||||
MyList *nums = malloc(sizeof(MyList));
|
||||
nums->capacity = 10;
|
||||
nums->arr = malloc(sizeof(int) * nums->capacity);
|
||||
nums->size = 0;
|
||||
nums->extendRatio = 2;
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
void delMyList(MyList *nums) {
|
||||
free(nums->arr);
|
||||
free(nums);
|
||||
}
|
||||
|
||||
/* Get list length */
|
||||
int size(MyList *nums) {
|
||||
return nums->size;
|
||||
}
|
||||
|
||||
/* Get list capacity */
|
||||
int capacity(MyList *nums) {
|
||||
return nums->capacity;
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
int get(MyList *nums, int index) {
|
||||
assert(index >= 0 && index < nums->size);
|
||||
return nums->arr[index];
|
||||
}
|
||||
|
||||
/* Add elements at the end */
|
||||
void set(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < nums->size);
|
||||
nums->arr[index] = num;
|
||||
}
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
void add(MyList *nums, int num) {
|
||||
if (size(nums) == capacity(nums)) {
|
||||
extendCapacity(nums); // Expand capacity
|
||||
}
|
||||
nums->arr[size(nums)] = num;
|
||||
nums->size++;
|
||||
}
|
||||
|
||||
/* Sort list */
|
||||
void insert(MyList *nums, int index, int num) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (size(nums) == capacity(nums)) {
|
||||
extendCapacity(nums); // Expand capacity
|
||||
}
|
||||
for (int i = size(nums); i > index; --i) {
|
||||
nums->arr[i] = nums->arr[i - 1];
|
||||
}
|
||||
nums->arr[index] = num;
|
||||
nums->size++;
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
// Note: stdio.h occupies the remove keyword
|
||||
int removeItem(MyList *nums, int index) {
|
||||
assert(index >= 0 && index < size(nums));
|
||||
int num = nums->arr[index];
|
||||
for (int i = index; i < size(nums) - 1; i++) {
|
||||
nums->arr[i] = nums->arr[i + 1];
|
||||
}
|
||||
nums->size--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void extendCapacity(MyList *nums) {
|
||||
// Allocate space first
|
||||
int newCapacity = capacity(nums) * nums->extendRatio;
|
||||
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
||||
int *temp = nums->arr;
|
||||
|
||||
// Copy old data to new data
|
||||
for (int i = 0; i < size(nums); i++)
|
||||
extend[i] = nums->arr[i];
|
||||
|
||||
// Free old data
|
||||
free(temp);
|
||||
|
||||
// Update new data
|
||||
nums->arr = extend;
|
||||
nums->capacity = newCapacity;
|
||||
}
|
||||
|
||||
/* Convert list to Array for printing */
|
||||
int *toArray(MyList *nums) {
|
||||
return nums->arr;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize list */
|
||||
MyList *nums = newMyList();
|
||||
/* Direct traversal of list elements */
|
||||
add(nums, 1);
|
||||
add(nums, 3);
|
||||
add(nums, 2);
|
||||
add(nums, 5);
|
||||
add(nums, 4);
|
||||
printf("List nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
printf("Capacity = %d, Length = %d\n", capacity(nums), size(nums));
|
||||
|
||||
/* Sort list */
|
||||
insert(nums, 3, 6);
|
||||
printf("Insert number 6 at index 3, resulting in nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
/* Remove element */
|
||||
removeItem(nums, 3);
|
||||
printf("Remove element at index 3, resulting in nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
/* Update element */
|
||||
int num = get(nums, 1);
|
||||
printf("Access element at index 1, get num = %d\n", num);
|
||||
|
||||
/* Add elements at the end */
|
||||
set(nums, 1, 0);
|
||||
printf("Update element at index 1 to 0, resulting in nums = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
|
||||
/* Test capacity expansion mechanism */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
add(nums, i);
|
||||
}
|
||||
|
||||
printf("List nums after expansion = ");
|
||||
printArray(toArray(nums), size(nums));
|
||||
printf("Capacity = %d, Length = %d\n", capacity(nums), size(nums));
|
||||
|
||||
/* Free allocated memory */
|
||||
delMyList(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user