mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-13 12:20:57 +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,72 @@
|
||||
/**
|
||||
* File: iteration.dart
|
||||
* Created Time: 2023-08-27
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* for loop */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while loop */
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int 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) */
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int 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 */
|
||||
String nestedForLoop(int n) {
|
||||
String res = "";
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// Loop j = 1, 2, ..., n-1, n
|
||||
for (int j = 1; j <= n; j++) {
|
||||
res += "($i, $j), ";
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
print("\nFor loop sum result res = $res");
|
||||
|
||||
res = whileLoop(n);
|
||||
print("\nWhile loop sum result res = $res");
|
||||
|
||||
res = whileLoopII(n);
|
||||
print("\nWhile loop (two updates) sum result res = $res");
|
||||
|
||||
String resStr = nestedForLoop(n);
|
||||
print("\nNested for loop result $resStr");
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* File: recursion.dart
|
||||
* Created Time: 2023-08-27
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* Recursion */
|
||||
int recur(int n) {
|
||||
// Termination condition
|
||||
if (n == 1) return 1;
|
||||
// Recurse: recursive call
|
||||
int res = recur(n - 1);
|
||||
// Return: return result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* Simulate recursion using iteration */
|
||||
int forLoopRecur(int n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
List<int> stack = [];
|
||||
int res = 0;
|
||||
// Recurse: recursive call
|
||||
for (int i = n; i > 0; i--) {
|
||||
// Simulate "recurse" with "push"
|
||||
stack.add(i);
|
||||
}
|
||||
// Return: return result
|
||||
while (!stack.isEmpty) {
|
||||
// Simulate "return" with "pop"
|
||||
res += stack.removeLast();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Tail recursion */
|
||||
int tailRecur(int n, int res) {
|
||||
// Termination condition
|
||||
if (n == 0) return res;
|
||||
// Tail recursive call
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: recursion */
|
||||
int fib(int 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)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// Return result f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
print("\nRecursion sum result res = $res");
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
print("\nTail recursion sum result res = $res");
|
||||
|
||||
res = forLoopRecur(n);
|
||||
print("\nUsing iteration to simulate recursion sum result res = $res");
|
||||
|
||||
res = fib(n);
|
||||
print("\nThe ${n}th Fibonacci number is $res");
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: space_complexity.dart
|
||||
* Created Time: 2023-2-12
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// ignore_for_file: unused_local_variable
|
||||
|
||||
import 'dart:collection';
|
||||
import '../utils/list_node.dart';
|
||||
import '../utils/print_util.dart';
|
||||
import '../utils/tree_node.dart';
|
||||
|
||||
/* Function */
|
||||
int function() {
|
||||
// Perform some operations
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
void constant(int n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
List<int> nums = List.filled(10000, 0);
|
||||
ListNode node = ListNode(0);
|
||||
// Variables in the loop occupy O(1) space
|
||||
for (var i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// Functions in the loop occupy O(1) space
|
||||
for (var i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
void linear(int n) {
|
||||
// Array of length n uses O(n) space
|
||||
List<int> nums = List.filled(n, 0);
|
||||
// A list of length n occupies O(n) space
|
||||
List<ListNode> nodes = [];
|
||||
for (var i = 0; i < n; i++) {
|
||||
nodes.add(ListNode(i));
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
Map<int, String> map = HashMap();
|
||||
for (var i = 0; i < n; i++) {
|
||||
map.putIfAbsent(i, () => i.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
void linearRecur(int n) {
|
||||
print('Recursion n = $n');
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
void quadratic(int n) {
|
||||
// Matrix uses O(n^2) space
|
||||
List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));
|
||||
// 2D list uses O(n^2) space
|
||||
List<List<int>> numList = [];
|
||||
for (var i = 0; i < n; i++) {
|
||||
List<int> tmp = [];
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.add(0);
|
||||
}
|
||||
numList.add(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
List<int> nums = List.filled(n, 0);
|
||||
print('In recursion n = $n, nums length = ${nums.length}');
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
TreeNode? buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
TreeNode root = TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
int n = 5;
|
||||
// Constant order
|
||||
constant(n);
|
||||
// Linear order
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// Exponential order
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// Exponential order
|
||||
TreeNode? root = buildTree(n);
|
||||
printTree(root);
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* File: time_complexity.dart
|
||||
* Created Time: 2023-02-12
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// ignore_for_file: unused_local_variable
|
||||
|
||||
/* Constant order */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (var i = 0; i < size; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (var i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order (traversing array) */
|
||||
int arrayTraversal(List<int> nums) {
|
||||
int count = 0;
|
||||
// Number of iterations is proportional to the array length
|
||||
for (var _num in nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// Number of iterations is quadratically related to the data size n
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic order (bubble sort) */
|
||||
int bubbleSort(List<int> nums) {
|
||||
int count = 0; // Counter
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (var 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 (var j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
int 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) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (var i = 0; i < n; i++) {
|
||||
for (var 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) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* Logarithmic order (loop implementation) */
|
||||
int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n ~/ 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Logarithmic order (recursive implementation) */
|
||||
int logRecur(int n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n ~/ 2) + 1;
|
||||
}
|
||||
|
||||
/* Linearithmic order */
|
||||
int linearLogRecur(int n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);
|
||||
for (var i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Factorial order (recursive implementation) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// Split from 1 into n
|
||||
for (var i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
// You can modify n to run and observe the trend of the number of operations for various complexities
|
||||
int n = 8;
|
||||
print('Input data size n = $n');
|
||||
|
||||
int count = constant(n);
|
||||
print('Constant-time operations count = $count');
|
||||
|
||||
count = linear(n);
|
||||
print('Linear-time operations count = $count');
|
||||
|
||||
count = arrayTraversal(List.filled(n, 0));
|
||||
print('Linear-time (array traversal) operations count = $count');
|
||||
|
||||
count = quadratic(n);
|
||||
print('Quadratic-time operations count = $count');
|
||||
final nums = List.filled(n, 0);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(nums);
|
||||
print('Quadratic-time (bubble sort) operations count = $count');
|
||||
|
||||
count = exponential(n);
|
||||
print('Exponential-time (iterative) operations count = $count');
|
||||
count = expRecur(n);
|
||||
print('Exponential-time (recursive) operations count = $count');
|
||||
|
||||
count = logarithmic(n);
|
||||
print('Logarithmic-time (iterative) operations count = $count');
|
||||
count = logRecur(n);
|
||||
print('Logarithmic-time (recursive) operations count = $count');
|
||||
|
||||
count = linearLogRecur(n);
|
||||
print('Linearithmic-time (recursive) operations count = $count');
|
||||
|
||||
count = factorialRecur(n);
|
||||
print('Factorial-time (recursive) operations count = $count');
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.dart
|
||||
* Created Time: 2023-02-12
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
List<int> randomNumbers(int n) {
|
||||
final nums = List.filled(n, 0);
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
for (var i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// Randomly shuffle array elements
|
||||
nums.shuffle();
|
||||
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* Find the index of number 1 in array nums */
|
||||
int findOne(List<int> nums) {
|
||||
for (var 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 */
|
||||
void main() {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
final nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
print('\nArray [ 1, 2, ..., n ] after shuffling = $nums');
|
||||
print('Index of number 1 is + $index');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user