mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-02 10:34:35 +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:
@@ -10,40 +10,40 @@ import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class array {
|
||||
/* Random access to elements */
|
||||
/* Random access to element */
|
||||
static int randomAccess(int[] nums) {
|
||||
// Randomly select a number in the interval [0, nums.length)
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// Retrieve and return a random element
|
||||
// Retrieve and return the random element
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
static int[] extend(int[] nums, int enlarge) {
|
||||
// Initialize an extended length array
|
||||
// Initialize an array with extended length
|
||||
int[] res = new int[nums.length + enlarge];
|
||||
// Copy all elements from the original array to the new array
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 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 */
|
||||
static void insert(int[] nums, 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 = nums.length - 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 */
|
||||
static void remove(int[] nums, 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 < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
@@ -56,13 +56,13 @@ public class array {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// Traverse array elements
|
||||
// Direct traversal of array elements
|
||||
for (int num : nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Search for a specified element in the array */
|
||||
/* Find the specified element in the array */
|
||||
static int find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target)
|
||||
@@ -73,33 +73,33 @@ public class array {
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* Initialize an array */
|
||||
/* Initialize array */
|
||||
int[] arr = new int[5];
|
||||
System.out.println("Array arr = " + Arrays.toString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
System.out.println("Array nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Random access */
|
||||
/* Insert element */
|
||||
int randomNum = randomAccess(nums);
|
||||
System.out.println("Get a random element from nums = " + randomNum);
|
||||
System.out.println("Get random element in nums " + randomNum);
|
||||
|
||||
/* Length extension */
|
||||
/* Traverse array */
|
||||
nums = extend(nums, 3);
|
||||
System.out.println("Extend the array length to 8, resulting in nums = " + Arrays.toString(nums));
|
||||
System.out.println("Extend array length to 8, resulting in nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Insert element */
|
||||
insert(nums, 6, 3);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums));
|
||||
System.out.println("Insert number 6 at index 3, resulting in nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Remove element */
|
||||
remove(nums, 2);
|
||||
System.out.println("Remove the element at index 2, resulting in nums = " + Arrays.toString(nums));
|
||||
System.out.println("Remove element at index 2, resulting in nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums);
|
||||
|
||||
/* Search for elements */
|
||||
/* Find element */
|
||||
int index = find(nums, 3);
|
||||
System.out.println("Find element 3 in nums, index = " + index);
|
||||
System.out.println("Find element 3 in nums, get index = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class linked_list {
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* Access the node at `index` in the linked list */
|
||||
/* Access the node at index index in the linked list */
|
||||
static ListNode access(ListNode head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
@@ -36,7 +36,7 @@ public class linked_list {
|
||||
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 */
|
||||
static int find(ListNode head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
@@ -62,25 +62,25 @@ public class linked_list {
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
System.out.println("The initialized linked list is");
|
||||
System.out.println("Initialized linked list is");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* Insert node */
|
||||
insert(n0, new ListNode(0));
|
||||
System.out.println("Linked list after inserting the node is");
|
||||
System.out.println("Linked list after inserting node is");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* Remove node */
|
||||
remove(n0);
|
||||
System.out.println("Linked list after removing the node is");
|
||||
System.out.println("Linked list after removing node is");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* Access node */
|
||||
ListNode node = access(n0, 3);
|
||||
System.out.println("The value of the node at index 3 in the linked list = " + node.val);
|
||||
System.out.println("Value of node at index 3 in linked list = " + node.val);
|
||||
|
||||
/* Search node */
|
||||
int index = find(n0, 2);
|
||||
System.out.println("The index of the node with value 2 in the linked list = " + index);
|
||||
System.out.println("Index of node with value 2 in linked list = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,24 +11,24 @@ import java.util.*;
|
||||
public class list {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize list */
|
||||
// The array's element type is Integer[], a wrapper class for int
|
||||
// Note that the array element type is Integer[], the wrapper class of int[]
|
||||
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
||||
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
|
||||
System.out.println("List nums = " + nums);
|
||||
|
||||
/* Access element */
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access the element at index 1, obtained num = " + num);
|
||||
|
||||
/* Update element */
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access element at index 1, get num = " + num);
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.set(1, 0);
|
||||
System.out.println("Update the element at index 1 to 0, resulting in nums = " + nums);
|
||||
System.out.println("Update element at index 1 to 0, resulting in nums = " + nums);
|
||||
|
||||
/* Clear list */
|
||||
/* Remove element */
|
||||
nums.clear();
|
||||
System.out.println("After clearing the list, nums = " + nums);
|
||||
System.out.println("After clearing list, nums = " + nums);
|
||||
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@@ -36,20 +36,20 @@ public class list {
|
||||
nums.add(4);
|
||||
System.out.println("After adding elements, nums = " + nums);
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
nums.add(3, 6);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + nums);
|
||||
System.out.println("Insert number 6 at index 3, resulting in nums = " + nums);
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
System.out.println("Remove the element at index 3, resulting in nums = " + nums);
|
||||
System.out.println("Remove element at index 3, resulting in nums = " + nums);
|
||||
|
||||
/* Traverse the list by index */
|
||||
/* Traverse list by index */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums.get(i);
|
||||
}
|
||||
/* Traverse the list elements */
|
||||
/* Directly traverse list elements */
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
@@ -61,6 +61,6 @@ public class list {
|
||||
|
||||
/* Sort list */
|
||||
Collections.sort(nums);
|
||||
System.out.println("After sorting the list, nums = " + nums);
|
||||
System.out.println("After sorting list, nums = " + nums);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class MyList {
|
||||
private int[] arr; // Array (stores list elements)
|
||||
private int capacity = 10; // List capacity
|
||||
private int size = 0; // List length (current number of elements)
|
||||
private int extendRatio = 2; // Multiple for each list expansion
|
||||
private int extendRatio = 2; // Multiple by which the list capacity is extended each time
|
||||
|
||||
/* Constructor */
|
||||
public MyList() {
|
||||
@@ -30,7 +30,7 @@ class MyList {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
public int get(int index) {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if (index < 0 || index >= size)
|
||||
@@ -38,16 +38,16 @@ class MyList {
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
/* Add elements at the end */
|
||||
public void set(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
public 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;
|
||||
@@ -55,14 +55,14 @@ class MyList {
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("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];
|
||||
}
|
||||
@@ -76,7 +76,7 @@ class MyList {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
int num = arr[index];
|
||||
// Move all elements after `index` one position forward
|
||||
// Move all elements after index forward by one position
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
@@ -86,18 +86,18 @@ class MyList {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Extend list */
|
||||
/* Driver Code */
|
||||
public void extendCapacity() {
|
||||
// Create a new array with a length multiple of the original array by extendRatio, and copy the original array to the new array
|
||||
// Create a new array with length extendRatio times the original array and copy the original array to the new array
|
||||
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
||||
// Update list capacity
|
||||
// Add elements at the end
|
||||
capacity = arr.length;
|
||||
}
|
||||
|
||||
/* Convert the list to an array */
|
||||
/* Convert list to array */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = get(i);
|
||||
@@ -111,7 +111,7 @@ public class my_list {
|
||||
public static void main(String[] args) {
|
||||
/* 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);
|
||||
@@ -120,28 +120,28 @@ public class my_list {
|
||||
System.out.println("List nums = " + Arrays.toString(nums.toArray()) +
|
||||
", capacity = " + nums.capacity() + ", length = " + nums.size());
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
nums.insert(3, 6);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
System.out.println("Insert number 6 at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
System.out.println("Remove the element at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Access element */
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access the element at index 1, obtained num = " + num);
|
||||
System.out.println("Remove element at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Update element */
|
||||
nums.set(1, 0);
|
||||
System.out.println("Update the element at index 1 to 0, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access element at index 1, get num = " + num);
|
||||
|
||||
/* Test expansion mechanism */
|
||||
/* Add elements at the end */
|
||||
nums.set(1, 0);
|
||||
System.out.println("Update element at index 1 to 0, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 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);
|
||||
}
|
||||
System.out.println("After extending, list nums = " + Arrays.toString(nums.toArray()) +
|
||||
System.out.println("List nums after expansion = " + Arrays.toString(nums.toArray()) +
|
||||
", capacity = " + nums.capacity() + ", length = " + nums.size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user