mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 19:54:20 +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:
@@ -6,18 +6,18 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Random access to elements */
|
||||
/* Random access to element */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// Randomly select a number in the range [0, size)
|
||||
// Randomly select a number from interval [0, size)
|
||||
int randomIndex = rand() % size;
|
||||
// Retrieve and return a random element
|
||||
// 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 extended length array
|
||||
// Initialize an array with extended length
|
||||
int *res = new int[size + enlarge];
|
||||
// Copy all elements from the original array to the new array
|
||||
for (int i = 0; i < size; i++) {
|
||||
@@ -25,23 +25,23 @@ int *extend(int *nums, int size, int enlarge) {
|
||||
}
|
||||
// Free memory
|
||||
delete[] nums;
|
||||
// Return the new array after expansion
|
||||
// Return the extended new array
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Insert element num at `index` */
|
||||
/* Insert element num at index index in the array */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// Move all elements after `index` one position backward
|
||||
// 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
|
||||
// Assign num to the element at index index
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* Remove the element at `index` */
|
||||
/* Remove the element at index index */
|
||||
void remove(int *nums, int size, int index) {
|
||||
// Move all elements after `index` one position forward
|
||||
// Move all elements after index index forward by one position
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
@@ -56,7 +56,7 @@ void traverse(int *nums, int size) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Search for a specified element in the array */
|
||||
/* 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)
|
||||
@@ -67,7 +67,7 @@ int find(int *nums, int size, int target) {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize an array */
|
||||
/* Initialize array */
|
||||
int size = 5;
|
||||
int *arr = new int[size];
|
||||
cout << "Array arr = ";
|
||||
@@ -77,33 +77,33 @@ int main() {
|
||||
cout << "Array nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* Random access */
|
||||
/* Insert element */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
cout << "Get a random element from nums = " << randomNum << endl;
|
||||
cout << "Get random element in nums " << randomNum << endl;
|
||||
|
||||
/* Length extension */
|
||||
/* Traverse array */
|
||||
int enlarge = 3;
|
||||
nums = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
cout << "Extend the array length to 8, resulting in nums = ";
|
||||
cout << "Extend array length to 8, resulting in nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* Insert element */
|
||||
insert(nums, size, 6, 3);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
cout << "Insert number 6 at index 3, resulting in nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* Remove element */
|
||||
remove(nums, size, 2);
|
||||
cout << "Remove the element at index 2, resulting in nums = ";
|
||||
cout << "Remove element at index 2, resulting in nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums, size);
|
||||
|
||||
/* Search for elements */
|
||||
/* Find element */
|
||||
int index = find(nums, size, 3);
|
||||
cout << "Find element 3 in nums, index = " << index << endl;
|
||||
cout << "Find element 3 in nums, get index = " << index << endl;
|
||||
|
||||
// Free memory
|
||||
delete[] arr;
|
||||
|
||||
@@ -25,7 +25,7 @@ void remove(ListNode *n0) {
|
||||
delete P;
|
||||
}
|
||||
|
||||
/* Access the node at `index` in the linked list */
|
||||
/* 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 == nullptr)
|
||||
@@ -35,7 +35,7 @@ ListNode *access(ListNode *head, int index) {
|
||||
return head;
|
||||
}
|
||||
|
||||
/* Search for the first node with value target in the linked list */
|
||||
/* Find the first node with value target in the linked list */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head != nullptr) {
|
||||
@@ -61,26 +61,26 @@ int main() {
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
cout << "The initialized linked list is" << endl;
|
||||
cout << "Initialized linked list is" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Insert node */
|
||||
insert(n0, new ListNode(0));
|
||||
cout << "Linked list after inserting the node is" << endl;
|
||||
cout << "Linked list after inserting node is" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Remove node */
|
||||
remove(n0);
|
||||
cout << "Linked list after removing the node is" << endl;
|
||||
cout << "Linked list after removing node is" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Access node */
|
||||
ListNode *node = access(n0, 3);
|
||||
cout << "The value of the node at index 3 in the linked list = " << node->val << endl;
|
||||
cout << "Value of node at index 3 in linked list = " << node->val << endl;
|
||||
|
||||
/* Search node */
|
||||
int index = find(n0, 2);
|
||||
cout << "The index of the node with value 2 in the linked list = " << index << endl;
|
||||
cout << "Index of node with value 2 in linked list = " << index << endl;
|
||||
|
||||
// Free memory
|
||||
freeMemoryLinkedList(n0);
|
||||
|
||||
@@ -13,21 +13,21 @@ int main() {
|
||||
cout << "List nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Access element */
|
||||
int num = nums[1];
|
||||
cout << "Access the element at index 1, obtained num = " << num << endl;
|
||||
|
||||
/* Update element */
|
||||
int num = nums[1];
|
||||
cout << "Access element at index 1, get num = " << num << endl;
|
||||
|
||||
/* Add elements at the end */
|
||||
nums[1] = 0;
|
||||
cout << "Update the element at index 1 to 0, resulting in nums = ";
|
||||
cout << "Update element at index 1 to 0, resulting in nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Clear list */
|
||||
/* Remove element */
|
||||
nums.clear();
|
||||
cout << "After clearing the list, nums = ";
|
||||
cout << "After clearing list, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
nums.push_back(1);
|
||||
nums.push_back(3);
|
||||
nums.push_back(2);
|
||||
@@ -36,22 +36,22 @@ int main() {
|
||||
cout << "After adding elements, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
nums.insert(nums.begin() + 3, 6);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
cout << "Insert number 6 at index 3, resulting in nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Remove element */
|
||||
nums.erase(nums.begin() + 3);
|
||||
cout << "Remove the element at index 3, resulting in nums = ";
|
||||
cout << "Remove element at index 3, resulting in nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Traverse the list by index */
|
||||
/* Traverse list by index */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* Traverse the list elements */
|
||||
/* Directly traverse list elements */
|
||||
count = 0;
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
@@ -65,7 +65,7 @@ int main() {
|
||||
|
||||
/* Sort list */
|
||||
sort(nums.begin(), nums.end());
|
||||
cout << "After sorting the list, nums = ";
|
||||
cout << "After sorting list, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -12,7 +12,7 @@ class MyList {
|
||||
int *arr; // Array (stores list elements)
|
||||
int arrCapacity = 10; // List capacity
|
||||
int arrSize = 0; // List length (current number of elements)
|
||||
int extendRatio = 2; // Multiple for each list expansion
|
||||
int extendRatio = 2; // Multiple by which the list capacity is extended each time
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -35,7 +35,7 @@ class MyList {
|
||||
return arrCapacity;
|
||||
}
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int get(int index) {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if (index < 0 || index >= size())
|
||||
@@ -43,16 +43,16 @@ class MyList {
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
/* Add elements at the end */
|
||||
void set(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
void add(int num) {
|
||||
// When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
arr[size()] = num;
|
||||
@@ -60,14 +60,14 @@ class MyList {
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
void insert(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
// When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
// Move all elements after `index` one position backward
|
||||
// Move all elements after index index forward by one position
|
||||
for (int j = size() - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
@@ -81,7 +81,7 @@ class MyList {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
int num = arr[index];
|
||||
// Move all elements after `index` one position forward
|
||||
// Create a new array with length _extend_ratio times the original array, and copy the original array to the new array
|
||||
for (int j = index; j < size() - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
@@ -91,9 +91,9 @@ class MyList {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Extend list */
|
||||
/* Driver Code */
|
||||
void extendCapacity() {
|
||||
// Create a new array with a length multiple of the original array by extendRatio
|
||||
// Create a new array with length extendRatio times the original array
|
||||
int newCapacity = capacity() * extendRatio;
|
||||
int *tmp = arr;
|
||||
arr = new int[newCapacity];
|
||||
@@ -106,9 +106,9 @@ class MyList {
|
||||
arrCapacity = newCapacity;
|
||||
}
|
||||
|
||||
/* Convert the list to a Vector for printing */
|
||||
/* Convert list to Vector for printing */
|
||||
vector<int> toVector() {
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
vector<int> vec(size());
|
||||
for (int i = 0; i < size(); i++) {
|
||||
vec[i] = arr[i];
|
||||
@@ -121,7 +121,7 @@ class MyList {
|
||||
int main() {
|
||||
/* Initialize list */
|
||||
MyList *nums = new MyList();
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
nums->add(1);
|
||||
nums->add(3);
|
||||
nums->add(2);
|
||||
@@ -132,34 +132,34 @@ int main() {
|
||||
printVector(vec);
|
||||
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
nums->insert(3, 6);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
cout << "Insert number 6 at index 3, resulting in nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* Remove element */
|
||||
nums->remove(3);
|
||||
cout << "Remove the element at index 3, resulting in nums = ";
|
||||
cout << "Remove element at index 3, resulting in nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* Access element */
|
||||
int num = nums->get(1);
|
||||
cout << "Access the element at index 1, obtained num = " << num << endl;
|
||||
|
||||
/* Update element */
|
||||
int num = nums->get(1);
|
||||
cout << "Access element at index 1, get num = " << num << endl;
|
||||
|
||||
/* Add elements at the end */
|
||||
nums->set(1, 0);
|
||||
cout << "Update the element at index 1 to 0, resulting in nums = ";
|
||||
cout << "Update element at index 1 to 0, resulting in nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* Test expansion mechanism */
|
||||
/* 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 at this time
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
nums->add(i);
|
||||
}
|
||||
cout << "After extending, list nums = ";
|
||||
cout << "List nums after expansion = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
|
||||
|
||||
Reference in New Issue
Block a user