mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 14:36:06 +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,107 @@
|
||||
// File: array.cs
|
||||
// Created Time: 2022-12-14
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class array {
|
||||
/* Random access to element */
|
||||
int RandomAccess(int[] nums) {
|
||||
Random random = new();
|
||||
// Randomly select a number in interval [0, nums.Length)
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
// Retrieve and return the random element
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
int[] Extend(int[] nums, int enlarge) {
|
||||
// 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 extended new array
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Insert element num at index index in the array */
|
||||
void Insert(int[] nums, int num, int index) {
|
||||
// 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 index
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* Remove the element at index index */
|
||||
void Remove(int[] nums, int index) {
|
||||
// Move all elements after index index forward by one position
|
||||
for (int i = index; i < nums.Length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* Traverse array */
|
||||
void Traverse(int[] nums) {
|
||||
int count = 0;
|
||||
// Traverse array by index
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// Direct traversal of array elements
|
||||
foreach (int num in nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the specified element in the array */
|
||||
int Find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Helper function, convert array to string */
|
||||
string ToString(int[] nums) {
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Initialize array
|
||||
int[] arr = new int[5];
|
||||
Console.WriteLine("Array arr = " + ToString(arr));
|
||||
int[] nums = [1, 3, 2, 5, 4];
|
||||
Console.WriteLine("Array nums = " + ToString(nums));
|
||||
|
||||
// Insert element
|
||||
int randomNum = RandomAccess(nums);
|
||||
Console.WriteLine("Get random element in nums " + randomNum);
|
||||
|
||||
// Traverse array
|
||||
nums = Extend(nums, 3);
|
||||
Console.WriteLine("Extend array length to 8, resulting in nums = " + ToString(nums));
|
||||
|
||||
// Insert element
|
||||
Insert(nums, 6, 3);
|
||||
Console.WriteLine("Insert number 6 at index 3, resulting in nums = " + ToString(nums));
|
||||
|
||||
// Remove element
|
||||
Remove(nums, 2);
|
||||
Console.WriteLine("Remove element at index 2, resulting in nums = " + ToString(nums));
|
||||
|
||||
// Traverse array
|
||||
Traverse(nums);
|
||||
|
||||
// Find element
|
||||
int index = Find(nums, 3);
|
||||
Console.WriteLine("Find element 3 in nums, get index = " + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// File: linked_list.cs
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class linked_list {
|
||||
/* 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 */
|
||||
void Remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 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 != null) {
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Initialize linked list
|
||||
// Initialize each node
|
||||
ListNode n0 = new(1);
|
||||
ListNode n1 = new(3);
|
||||
ListNode n2 = new(2);
|
||||
ListNode n3 = new(5);
|
||||
ListNode n4 = new(4);
|
||||
// Build references between nodes
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
Console.WriteLine($"Initialized linked list is{n0}");
|
||||
|
||||
// Insert node
|
||||
Insert(n0, new ListNode(0));
|
||||
Console.WriteLine($"Linked list after node insertion is{n0}");
|
||||
|
||||
// Remove node
|
||||
Remove(n0);
|
||||
Console.WriteLine($"Linked list after node deletion is{n0}");
|
||||
|
||||
// Access node
|
||||
ListNode? node = Access(n0, 3);
|
||||
Console.WriteLine($"Value of node at index 3 in linked list = {node?.val}");
|
||||
|
||||
// Search node
|
||||
int index = Find(n0, 2);
|
||||
Console.WriteLine($"Index of node with value 2 in linked list = {index}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: list.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class list {
|
||||
[Test]
|
||||
public void Test() {
|
||||
|
||||
/* Initialize list */
|
||||
int[] numbers = [1, 3, 2, 5, 4];
|
||||
List<int> nums = [.. numbers];
|
||||
Console.WriteLine("List nums = " + string.Join(",", nums));
|
||||
|
||||
/* Update element */
|
||||
int num = nums[1];
|
||||
Console.WriteLine("Access element at index 1, get num = " + num);
|
||||
|
||||
/* Add elements at the end */
|
||||
nums[1] = 0;
|
||||
Console.WriteLine("Update element at index 1 to 0, resulting in nums = " + string.Join(",", nums));
|
||||
|
||||
/* Remove element */
|
||||
nums.Clear();
|
||||
Console.WriteLine("After clearing list, nums = " + string.Join(",", nums));
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
nums.Add(1);
|
||||
nums.Add(3);
|
||||
nums.Add(2);
|
||||
nums.Add(5);
|
||||
nums.Add(4);
|
||||
Console.WriteLine("After adding elements, nums = " + string.Join(",", nums));
|
||||
|
||||
/* Sort list */
|
||||
nums.Insert(3, 6);
|
||||
Console.WriteLine("Insert number 6 at index 3, resulting in nums = " + string.Join(",", nums));
|
||||
|
||||
/* Remove element */
|
||||
nums.RemoveAt(3);
|
||||
Console.WriteLine("Remove element at index 3, resulting in nums = " + string.Join(",", nums));
|
||||
|
||||
/* Traverse list by index */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.Count; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* Directly traverse list elements */
|
||||
count = 0;
|
||||
foreach (int x in nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* Concatenate two lists */
|
||||
List<int> nums1 = [6, 8, 7, 10, 9];
|
||||
nums.AddRange(nums1);
|
||||
Console.WriteLine("Concatenate list nums1 to nums, resulting in nums = " + string.Join(",", nums));
|
||||
|
||||
/* Sort list */
|
||||
nums.Sort(); // After sorting, list elements are arranged from smallest to largest
|
||||
Console.WriteLine("After sorting list, nums = " + string.Join(",", nums));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* File: my_list.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
/* List class */
|
||||
class MyList {
|
||||
private int[] arr; // Array (stores list elements)
|
||||
private int arrCapacity = 10; // List capacity
|
||||
private int arrSize = 0; // List length (current number of elements)
|
||||
private readonly int extendRatio = 2; // Multiple by which the list capacity is extended each time
|
||||
|
||||
/* Constructor */
|
||||
public MyList() {
|
||||
arr = new int[arrCapacity];
|
||||
}
|
||||
|
||||
/* Get list length (current number of elements) */
|
||||
public int Size() {
|
||||
return arrSize;
|
||||
}
|
||||
|
||||
/* Get list capacity */
|
||||
public int Capacity() {
|
||||
return arrCapacity;
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
public int Get(int index) {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("Index out of bounds");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* Add elements at the end */
|
||||
public void Set(int index, int num) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("Index out of bounds");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
public void Add(int num) {
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (arrSize == arrCapacity)
|
||||
ExtendCapacity();
|
||||
arr[arrSize] = num;
|
||||
// Update the number of elements
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* Sort list */
|
||||
public void Insert(int index, int num) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("Index out of bounds");
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (arrSize == arrCapacity)
|
||||
ExtendCapacity();
|
||||
// Move all elements after index index forward by one position
|
||||
for (int j = arrSize - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
arr[index] = num;
|
||||
// Update the number of elements
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
public int Remove(int index) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("Index out of bounds");
|
||||
int num = arr[index];
|
||||
// Move all elements after index forward by one position
|
||||
for (int j = index; j < arrSize - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// Update the number of elements
|
||||
arrSize--;
|
||||
// Return the removed element
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public void ExtendCapacity() {
|
||||
// Create new array of length arrCapacity * extendRatio and copy original array to new array
|
||||
Array.Resize(ref arr, arrCapacity * extendRatio);
|
||||
// Add elements at the end
|
||||
arrCapacity = arr.Length;
|
||||
}
|
||||
|
||||
/* Convert list to array */
|
||||
public int[] ToArray() {
|
||||
// Elements enqueue
|
||||
int[] arr = new int[arrSize];
|
||||
for (int i = 0; i < arrSize; i++) {
|
||||
arr[i] = Get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
public class my_list {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize list */
|
||||
MyList nums = new();
|
||||
/* Direct traversal of list elements */
|
||||
nums.Add(1);
|
||||
nums.Add(3);
|
||||
nums.Add(2);
|
||||
nums.Add(5);
|
||||
nums.Add(4);
|
||||
Console.WriteLine("List nums = " + string.Join(",", nums.ToArray()) +
|
||||
", capacity = " + nums.Capacity() + ", length = " + nums.Size());
|
||||
|
||||
/* Sort list */
|
||||
nums.Insert(3, 6);
|
||||
Console.WriteLine("Insert number 6 at index 3, resulting in nums = " + string.Join(",", nums.ToArray()));
|
||||
|
||||
/* Remove element */
|
||||
nums.Remove(3);
|
||||
Console.WriteLine("Remove element at index 3, resulting in nums = " + string.Join(",", nums.ToArray()));
|
||||
|
||||
/* Update element */
|
||||
int num = nums.Get(1);
|
||||
Console.WriteLine("Access element at index 1, get num = " + num);
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.Set(1, 0);
|
||||
Console.WriteLine("Update element at index 1 to 0, resulting in nums = " + string.Join(",", 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
|
||||
nums.Add(i);
|
||||
}
|
||||
Console.WriteLine("List nums after expansion = " + string.Join(",", nums.ToArray()) +
|
||||
", capacity = " + nums.Capacity() + ", length = " + nums.Size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user