mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-02 18:44:22 +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,97 @@
|
||||
/**
|
||||
* File: array.js
|
||||
* Created Time: 2022-11-27
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
/* Random access to element */
|
||||
function randomAccess(nums) {
|
||||
// Randomly select a number in the interval [0, nums.length)
|
||||
const random_index = Math.floor(Math.random() * nums.length);
|
||||
// Retrieve and return the random element
|
||||
const random_num = nums[random_index];
|
||||
return random_num;
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
// Note: JavaScript's Array is dynamic array, can be directly expanded
|
||||
// For learning purposes, this function treats Array as fixed-length array
|
||||
function extend(nums, enlarge) {
|
||||
// Initialize an array with extended length
|
||||
const res = new Array(nums.length + enlarge).fill(0);
|
||||
// Copy all elements from the original array to the new array
|
||||
for (let 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 */
|
||||
function insert(nums, num, index) {
|
||||
// Move all elements at and after index index backward by one position
|
||||
for (let 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 */
|
||||
function remove(nums, index) {
|
||||
// Move all elements after index index forward by one position
|
||||
for (let i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* Traverse array */
|
||||
function traverse(nums) {
|
||||
let count = 0;
|
||||
// Traverse array by index
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// Direct traversal of array elements
|
||||
for (const num of nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the specified element in the array */
|
||||
function find(nums, target) {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (nums[i] === target) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize array */
|
||||
const arr = new Array(5).fill(0);
|
||||
console.log('Array arr =', arr);
|
||||
let nums = [1, 3, 2, 5, 4];
|
||||
console.log('Array nums =', nums);
|
||||
|
||||
/* Insert element */
|
||||
let random_num = randomAccess(nums);
|
||||
console.log('Get random element in nums', random_num);
|
||||
|
||||
/* Traverse array */
|
||||
nums = extend(nums, 3);
|
||||
console.log('Extend array length to 8, get nums =', nums);
|
||||
|
||||
/* Insert element */
|
||||
insert(nums, 6, 3);
|
||||
console.log('Insert number 6 at index 3, get nums =', nums);
|
||||
|
||||
/* Remove element */
|
||||
remove(nums, 2);
|
||||
console.log('Remove element at index 2, get nums =', nums);
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums);
|
||||
|
||||
/* Find element */
|
||||
let index = find(nums, 3);
|
||||
console.log('Find element 3 in nums, get index =', index);
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* File: linked_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: IsChristina (christinaxia77@foxmail.com), Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { printLinkedList } = require('../modules/PrintUtil');
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* Insert node P after node n0 in the linked list */
|
||||
function insert(n0, P) {
|
||||
const n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* Remove the first node after node n0 in the linked list */
|
||||
function remove(n0) {
|
||||
if (!n0.next) return;
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* Access the node at index index in the linked list */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
if (!head) {
|
||||
return null;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* Find the first node with value target in the linked list */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize linked list */
|
||||
// Initialize each node
|
||||
const n0 = new ListNode(1);
|
||||
const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// Build references between nodes
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
console.log('Initialized linked list is');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Insert node */
|
||||
insert(n0, new ListNode(0));
|
||||
console.log('Linked list after inserting node is');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Remove node */
|
||||
remove(n0);
|
||||
console.log('Linked list after removing node is');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Access node */
|
||||
const node = access(n0, 3);
|
||||
console.log('Value of node at index 3 in linked list = ' + node.val);
|
||||
|
||||
/* Search node */
|
||||
const index = find(n0, 2);
|
||||
console.log('Index of node with value 2 in linked list = ' + index);
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* File: list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Initialize list */
|
||||
const nums = [1, 3, 2, 5, 4];
|
||||
console.log(`List nums = ${nums}`);
|
||||
|
||||
/* Update element */
|
||||
const num = nums[1];
|
||||
console.log(`Access element at index 1, get num = ${num}`);
|
||||
|
||||
/* Add elements at the end */
|
||||
nums[1] = 0;
|
||||
console.log(`Update element at index 1 to 0, get nums = ${nums}`);
|
||||
|
||||
/* Remove element */
|
||||
nums.length = 0;
|
||||
console.log(`After clearing list, nums = ${nums}`);
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
nums.push(5);
|
||||
nums.push(4);
|
||||
console.log(`After adding elements, nums = ${nums}`);
|
||||
|
||||
/* Sort list */
|
||||
nums.splice(3, 0, 6);
|
||||
console.log(`Insert number 6 at index 3, get nums = ${nums}`);
|
||||
|
||||
/* Remove element */
|
||||
nums.splice(3, 1);
|
||||
console.log(`Delete element at index 3, get nums = ${nums}`);
|
||||
|
||||
/* Traverse list by index */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* Directly traverse list elements */
|
||||
count = 0;
|
||||
for (const x of nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* Concatenate two lists */
|
||||
const nums1 = [6, 8, 7, 10, 9];
|
||||
nums.push(...nums1);
|
||||
console.log(`After concatenating list nums1 to nums, get nums = ${nums}`);
|
||||
|
||||
/* Sort list */
|
||||
nums.sort((a, b) => a - b);
|
||||
console.log(`After sorting list, nums = ${nums}`);
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: my_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* List class */
|
||||
class MyList {
|
||||
#arr = new Array(); // Array (stores list elements)
|
||||
#capacity = 10; // List capacity
|
||||
#size = 0; // List length (current number of elements)
|
||||
#extendRatio = 2; // Multiple by which the list capacity is extended each time
|
||||
|
||||
/* Constructor */
|
||||
constructor() {
|
||||
this.#arr = new Array(this.#capacity);
|
||||
}
|
||||
|
||||
/* Get list length (current number of elements) */
|
||||
size() {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
/* Get list capacity */
|
||||
capacity() {
|
||||
return this.#capacity;
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
get(index) {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');
|
||||
return this.#arr[index];
|
||||
}
|
||||
|
||||
/* Add elements at the end */
|
||||
set(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');
|
||||
this.#arr[index] = num;
|
||||
}
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
add(num) {
|
||||
// If length equals capacity, need to expand
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// Add new element to end of list
|
||||
this.#arr[this.#size] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* Sort list */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// Move all elements after index index forward by one position
|
||||
for (let j = this.#size - 1; j >= index; j--) {
|
||||
this.#arr[j + 1] = this.#arr[j];
|
||||
}
|
||||
// Update the number of elements
|
||||
this.#arr[index] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
remove(index) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');
|
||||
let num = this.#arr[index];
|
||||
// Create a new array with length _extend_ratio times the original array, and copy the original array to the new array
|
||||
for (let j = index; j < this.#size - 1; j++) {
|
||||
this.#arr[j] = this.#arr[j + 1];
|
||||
}
|
||||
// Update the number of elements
|
||||
this.#size--;
|
||||
// Return the removed element
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
extendCapacity() {
|
||||
// Create a new array with length extendRatio times the original array and copy the original array to the new array
|
||||
this.#arr = this.#arr.concat(
|
||||
new Array(this.capacity() * (this.#extendRatio - 1))
|
||||
);
|
||||
// Add elements at the end
|
||||
this.#capacity = this.#arr.length;
|
||||
}
|
||||
|
||||
/* Convert list to array */
|
||||
toArray() {
|
||||
let size = this.size();
|
||||
// Elements enqueue
|
||||
const arr = new Array(size);
|
||||
for (let i = 0; i < size; i++) {
|
||||
arr[i] = this.get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize list */
|
||||
const nums = new MyList();
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
console.log(
|
||||
`List nums = ${nums.toArray()}, capacity = ${nums.capacity()}, length = ${nums.size()}`
|
||||
);
|
||||
|
||||
/* Sort list */
|
||||
nums.insert(3, 6);
|
||||
console.log(`Insert number 6 at index 3, get nums = ${nums.toArray()}`);
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
console.log(`Delete element at index 3, get nums = ${nums.toArray()}`);
|
||||
|
||||
/* Update element */
|
||||
const num = nums.get(1);
|
||||
console.log(`Access element at index 1, get num = ${num}`);
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.set(1, 0);
|
||||
console.log(`Update element at index 1 to 0, get nums = ${nums.toArray()}`);
|
||||
|
||||
/* Test capacity expansion mechanism */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
nums.add(i);
|
||||
}
|
||||
console.log(
|
||||
`After expansion, list nums = ${nums.toArray()}, capacity = ${nums.capacity()}, length = ${nums.size()}`
|
||||
);
|
||||
Reference in New Issue
Block a user