mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08: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,70 @@
|
||||
/**
|
||||
* File: iteration.js
|
||||
* Created Time: 2023-08-28
|
||||
* Author: Gaofer Chou (gaofer-chou@qq.com)
|
||||
*/
|
||||
|
||||
/* for loop */
|
||||
function forLoop(n) {
|
||||
let res = 0;
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
for (let i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while loop */
|
||||
function whileLoop(n) {
|
||||
let res = 0;
|
||||
let i = 1; // Initialize condition variable
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // Update condition variable
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while loop (two updates) */
|
||||
function whileLoopII(n) {
|
||||
let res = 0;
|
||||
let i = 1; // Initialize condition variable
|
||||
// Sum 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// Update condition variable
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Nested for loop */
|
||||
function nestedForLoop(n) {
|
||||
let res = '';
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
for (let i = 1; i <= n; i++) {
|
||||
// Loop j = 1, 2, ..., n-1, n
|
||||
for (let j = 1; j <= n; j++) {
|
||||
res += `(${i}, ${j}), `;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 5;
|
||||
let res;
|
||||
|
||||
res = forLoop(n);
|
||||
console.log(`For loop sum result res = ${res}`);
|
||||
|
||||
res = whileLoop(n);
|
||||
console.log(`While loop sum result res = ${res}`);
|
||||
|
||||
res = whileLoopII(n);
|
||||
console.log(`While loop (two updates) sum result res = ${res}`);
|
||||
|
||||
const resStr = nestedForLoop(n);
|
||||
console.log(`Nested for loop traversal result ${resStr}`);
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* File: recursion.js
|
||||
* Created Time: 2023-08-28
|
||||
* Author: Gaofer Chou (gaofer-chou@qq.com)
|
||||
*/
|
||||
|
||||
/* Recursion */
|
||||
function recur(n) {
|
||||
// Termination condition
|
||||
if (n === 1) return 1;
|
||||
// Recurse: recursive call
|
||||
const res = recur(n - 1);
|
||||
// Return: return result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* Simulate recursion using iteration */
|
||||
function forLoopRecur(n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
const stack = [];
|
||||
let res = 0;
|
||||
// Recurse: recursive call
|
||||
for (let i = n; i > 0; i--) {
|
||||
// Simulate "recurse" with "push"
|
||||
stack.push(i);
|
||||
}
|
||||
// Return: return result
|
||||
while (stack.length) {
|
||||
// Simulate "return" with "pop"
|
||||
res += stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Tail recursion */
|
||||
function tailRecur(n, res) {
|
||||
// Termination condition
|
||||
if (n === 0) return res;
|
||||
// Tail recursive call
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: recursion */
|
||||
function fib(n) {
|
||||
// Termination condition f(1) = 0, f(2) = 1
|
||||
if (n === 1 || n === 2) return n - 1;
|
||||
// Recursive call f(n) = f(n-1) + f(n-2)
|
||||
const res = fib(n - 1) + fib(n - 2);
|
||||
// Return result f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 5;
|
||||
let res;
|
||||
|
||||
res = recur(n);
|
||||
console.log(`Recursion sum result res = ${res}`);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
console.log(`Using iteration to simulate recursion sum result res = ${res}`);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
console.log(`Tail recursion sum result res = ${res}`);
|
||||
|
||||
res = fib(n);
|
||||
console.log(`The ${n}th Fibonacci number is ${res}`);
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* File: space_complexity.js
|
||||
* Created Time: 2023-02-05
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Function */
|
||||
function constFunc() {
|
||||
// Perform some operations
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
function constant(n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
const a = 0;
|
||||
const b = 0;
|
||||
const nums = new Array(10000);
|
||||
const node = new ListNode(0);
|
||||
// Variables in the loop occupy O(1) space
|
||||
for (let i = 0; i < n; i++) {
|
||||
const c = 0;
|
||||
}
|
||||
// Functions in the loop occupy O(1) space
|
||||
for (let i = 0; i < n; i++) {
|
||||
constFunc();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
function linear(n) {
|
||||
// Array of length n uses O(n) space
|
||||
const nums = new Array(n);
|
||||
// A list of length n occupies O(n) space
|
||||
const nodes = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
nodes.push(new ListNode(i));
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
const map = new Map();
|
||||
for (let i = 0; i < n; i++) {
|
||||
map.set(i, i.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
function linearRecur(n) {
|
||||
console.log(`Recursion n = ${n}`);
|
||||
if (n === 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
function quadratic(n) {
|
||||
// Matrix uses O(n^2) space
|
||||
const numMatrix = Array(n)
|
||||
.fill(null)
|
||||
.map(() => Array(n).fill(null));
|
||||
// 2D list uses O(n^2) space
|
||||
const numList = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const tmp = [];
|
||||
for (let j = 0; j < n; j++) {
|
||||
tmp.push(0);
|
||||
}
|
||||
numList.push(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
function quadraticRecur(n) {
|
||||
if (n <= 0) return 0;
|
||||
const nums = new Array(n);
|
||||
console.log(`In recursion n = ${n}, nums length = ${nums.length}`);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
function buildTree(n) {
|
||||
if (n === 0) return null;
|
||||
const root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 5;
|
||||
// Constant order
|
||||
constant(n);
|
||||
// Linear order
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// Exponential order
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// Exponential order
|
||||
const root = buildTree(n);
|
||||
printTree(root);
|
||||
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* File: time_complexity.js
|
||||
* Created Time: 2023-01-02
|
||||
* Author: RiverTwilight (contact@rene.wang)
|
||||
*/
|
||||
|
||||
/* Constant order */
|
||||
function constant(n) {
|
||||
let count = 0;
|
||||
const size = 100000;
|
||||
for (let i = 0; i < size; i++) count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
function linear(n) {
|
||||
let count = 0;
|
||||
for (let i = 0; i < n; i++) count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order (traversing array) */
|
||||
function arrayTraversal(nums) {
|
||||
let count = 0;
|
||||
// Number of iterations is proportional to the array length
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
function quadratic(n) {
|
||||
let count = 0;
|
||||
// Number of iterations is quadratically related to the data size n
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic order (bubble sort) */
|
||||
function bubbleSort(nums) {
|
||||
let count = 0; // Counter
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (let i = nums.length - 1; i > 0; i--) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (let j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
let tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // Element swap includes 3 unit operations
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order (loop implementation) */
|
||||
function exponential(n) {
|
||||
let count = 0,
|
||||
base = 1;
|
||||
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order (recursive implementation) */
|
||||
function expRecur(n) {
|
||||
if (n === 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* Logarithmic order (loop implementation) */
|
||||
function logarithmic(n) {
|
||||
let count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Logarithmic order (recursive implementation) */
|
||||
function logRecur(n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* Linearithmic order */
|
||||
function linearLogRecur(n) {
|
||||
if (n <= 1) return 1;
|
||||
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (let i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Factorial order (recursive implementation) */
|
||||
function factorialRecur(n) {
|
||||
if (n === 0) return 1;
|
||||
let count = 0;
|
||||
// Split from 1 into n
|
||||
for (let i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// You can modify n to run and observe the trend of the number of operations for various complexities
|
||||
const n = 8;
|
||||
console.log('Input data size n = ' + n);
|
||||
|
||||
let count = constant(n);
|
||||
console.log('Constant order operation count = ' + count);
|
||||
|
||||
count = linear(n);
|
||||
console.log('Linear order operation count = ' + count);
|
||||
count = arrayTraversal(new Array(n));
|
||||
console.log('Linear order (array traversal) operation count = ' + count);
|
||||
|
||||
count = quadratic(n);
|
||||
console.log('Quadratic order operation count = ' + count);
|
||||
let nums = new Array(n);
|
||||
for (let i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
console.log('Quadratic order (bubble sort) operation count = ' + count);
|
||||
|
||||
count = exponential(n);
|
||||
console.log('Exponential order (loop implementation) operation count = ' + count);
|
||||
count = expRecur(n);
|
||||
console.log('Exponential order (recursive implementation) operation count = ' + count);
|
||||
|
||||
count = logarithmic(n);
|
||||
console.log('Logarithmic order (loop implementation) operation count = ' + count);
|
||||
count = logRecur(n);
|
||||
console.log('Logarithmic order (recursive implementation) operation count = ' + count);
|
||||
|
||||
count = linearLogRecur(n);
|
||||
console.log('Linearithmic order (recursive implementation) operation count = ' + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
console.log('Factorial order (recursive implementation) operation count = ' + count);
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.js
|
||||
* Created Time: 2023-01-05
|
||||
* Author: RiverTwilight (contact@rene.wang)
|
||||
*/
|
||||
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
function randomNumbers(n) {
|
||||
const nums = Array(n);
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
for (let i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// Randomly shuffle array elements
|
||||
for (let i = 0; i < n; i++) {
|
||||
const r = Math.floor(Math.random() * (i + 1));
|
||||
const temp = nums[i];
|
||||
nums[i] = nums[r];
|
||||
nums[r] = temp;
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* Find the index of number 1 in array nums */
|
||||
function findOne(nums) {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
// When element 1 is at the head of the array, best time complexity O(1) is achieved
|
||||
// When element 1 is at the tail of the array, worst time complexity O(n) is achieved
|
||||
if (nums[i] === 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const n = 100;
|
||||
const nums = randomNumbers(n);
|
||||
const index = findOne(nums);
|
||||
console.log('\nArray [ 1, 2, ..., n ] after shuffling = [' + nums.join(', ') + ']');
|
||||
console.log('Index of number 1 is ' + index);
|
||||
}
|
||||
Reference in New Issue
Block a user