mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-11 03:10:58 +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,59 @@
|
||||
// File: iteration.go
|
||||
// Created Time: 2023-08-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import "fmt"
|
||||
|
||||
/* for loop */
|
||||
func forLoop(n int) int {
|
||||
res := 0
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
for i := 1; i <= n; i++ {
|
||||
res += i
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
/* while loop */
|
||||
func whileLoop(n int) int {
|
||||
res := 0
|
||||
// Initialize condition variable
|
||||
i := 1
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
for i <= n {
|
||||
res += i
|
||||
// Update condition variable
|
||||
i++
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
/* while loop (two updates) */
|
||||
func whileLoopII(n int) int {
|
||||
res := 0
|
||||
// Initialize condition variable
|
||||
i := 1
|
||||
// Sum 1, 4, 10, ...
|
||||
for i <= n {
|
||||
res += i
|
||||
// Update condition variable
|
||||
i++
|
||||
i *= 2
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
/* Nested for loop */
|
||||
func nestedForLoop(n int) string {
|
||||
res := ""
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
for i := 1; i <= n; i++ {
|
||||
for j := 1; j <= n; j++ {
|
||||
// Loop j = 1, 2, ..., n-1, n
|
||||
res += fmt.Sprintf("(%d, %d), ", i, j)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// File: iteration_test.go
|
||||
// Created Time: 2023-08-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestIteration(t *testing.T) {
|
||||
n := 5
|
||||
res := forLoop(n)
|
||||
fmt.Println("\nfor loop sum result res = ", res)
|
||||
|
||||
res = whileLoop(n)
|
||||
fmt.Println("\nwhile loop sum result res = ", res)
|
||||
|
||||
res = whileLoopII(n)
|
||||
fmt.Println("\nwhile loop (two updates) sum result res = ", res)
|
||||
|
||||
resStr := nestedForLoop(n)
|
||||
fmt.Println("\nDouble for loop traversal result ", resStr)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// File: recursion.go
|
||||
// Created Time: 2023-08-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import "container/list"
|
||||
|
||||
/* Recursion */
|
||||
func recur(n int) int {
|
||||
// Termination condition
|
||||
if n == 1 {
|
||||
return 1
|
||||
}
|
||||
// Recurse: recursive call
|
||||
res := recur(n - 1)
|
||||
// Return: return result
|
||||
return n + res
|
||||
}
|
||||
|
||||
/* Simulate recursion using iteration */
|
||||
func forLoopRecur(n int) int {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
stack := list.New()
|
||||
res := 0
|
||||
// Recurse: recursive call
|
||||
for i := n; i > 0; i-- {
|
||||
// Simulate "recurse" with "push"
|
||||
stack.PushBack(i)
|
||||
}
|
||||
// Return: return result
|
||||
for stack.Len() != 0 {
|
||||
// Simulate "return" with "pop"
|
||||
res += stack.Back().Value.(int)
|
||||
stack.Remove(stack.Back())
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res
|
||||
}
|
||||
|
||||
/* Tail recursion */
|
||||
func tailRecur(n int, res int) int {
|
||||
// Termination condition
|
||||
if n == 0 {
|
||||
return res
|
||||
}
|
||||
// Tail recursive call
|
||||
return tailRecur(n-1, res+n)
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: recursion */
|
||||
func fib(n int) int {
|
||||
// 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)
|
||||
res := fib(n-1) + fib(n-2)
|
||||
// Return result f(n)
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// File: recursion_test.go
|
||||
// Created Time: 2023-08-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestRecursion(t *testing.T) {
|
||||
n := 5
|
||||
res := recur(n)
|
||||
fmt.Println("\nRecursive function sum result res = ", res)
|
||||
|
||||
res = forLoopRecur(n)
|
||||
fmt.Println("\nUsing iteration to simulate recursive sum result res = ", res)
|
||||
|
||||
res = tailRecur(n, 0)
|
||||
fmt.Println("\nTail recursive function sum result res = ", res)
|
||||
|
||||
res = fib(n)
|
||||
fmt.Println("\nThe ", n, "th term of Fibonacci sequence is", res)
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
// File: space_complexity.go
|
||||
// Created Time: 2022-12-15
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
/* Struct */
|
||||
type node struct {
|
||||
val int
|
||||
next *node
|
||||
}
|
||||
|
||||
/* Create node struct */
|
||||
func newNode(val int) *node {
|
||||
return &node{val: val}
|
||||
}
|
||||
|
||||
/* Function */
|
||||
func function() int {
|
||||
// Perform some operations...
|
||||
return 0
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
func spaceConstant(n int) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
const a = 0
|
||||
b := 0
|
||||
nums := make([]int, 10000)
|
||||
node := newNode(0)
|
||||
// Variables in the loop occupy O(1) space
|
||||
var c int
|
||||
for i := 0; i < n; i++ {
|
||||
c = 0
|
||||
}
|
||||
// Functions in the loop occupy O(1) space
|
||||
for i := 0; i < n; i++ {
|
||||
function()
|
||||
}
|
||||
b += 0
|
||||
c += 0
|
||||
nums[0] = 0
|
||||
node.val = 0
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
func spaceLinear(n int) {
|
||||
// Array of length n uses O(n) space
|
||||
_ = make([]int, n)
|
||||
// A list of length n occupies O(n) space
|
||||
var nodes []*node
|
||||
for i := 0; i < n; i++ {
|
||||
nodes = append(nodes, newNode(i))
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
m := make(map[int]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
m[i] = strconv.Itoa(i)
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
func spaceLinearRecur(n int) {
|
||||
fmt.Println("Recursion n =", n)
|
||||
if n == 1 {
|
||||
return
|
||||
}
|
||||
spaceLinearRecur(n - 1)
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
func spaceQuadratic(n int) {
|
||||
// Matrix uses O(n^2) space
|
||||
numMatrix := make([][]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
numMatrix[i] = make([]int, n)
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
func spaceQuadraticRecur(n int) int {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
nums := make([]int, n)
|
||||
fmt.Printf("In recursion n = %d, nums length = %d \n", n, len(nums))
|
||||
return spaceQuadraticRecur(n - 1)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func buildTree(n int) *TreeNode {
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
root := NewTreeNode(0)
|
||||
root.Left = buildTree(n - 1)
|
||||
root.Right = buildTree(n - 1)
|
||||
return root
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// File: space_complexity_test.go
|
||||
// Created Time: 2022-12-15
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestSpaceComplexity(t *testing.T) {
|
||||
n := 5
|
||||
// Constant order
|
||||
spaceConstant(n)
|
||||
// Linear order
|
||||
spaceLinear(n)
|
||||
spaceLinearRecur(n)
|
||||
// Exponential order
|
||||
spaceQuadratic(n)
|
||||
spaceQuadraticRecur(n)
|
||||
// Exponential order
|
||||
root := buildTree(n)
|
||||
PrintTree(root)
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// File: time_complexity.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
/* Constant order */
|
||||
func constant(n int) int {
|
||||
count := 0
|
||||
size := 100000
|
||||
for i := 0; i < size; i++ {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
func linear(n int) int {
|
||||
count := 0
|
||||
for i := 0; i < n; i++ {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/* Linear order (traversing array) */
|
||||
func arrayTraversal(nums []int) int {
|
||||
count := 0
|
||||
// Number of iterations is proportional to the array length
|
||||
for range nums {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
func quadratic(n int) int {
|
||||
count := 0
|
||||
// Number of iterations is quadratically related to the data size n
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/* Quadratic order (bubble sort) */
|
||||
func bubbleSort(nums []int) int {
|
||||
count := 0 // Counter
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for i := len(nums) - 1; i > 0; i-- {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for j := 0; j < i; j++ {
|
||||
if nums[j] > nums[j+1] {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
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) */
|
||||
func exponential(n int) int {
|
||||
count, base := 0, 1
|
||||
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for i := 0; i < n; i++ {
|
||||
for 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) */
|
||||
func expRecur(n int) int {
|
||||
if n == 1 {
|
||||
return 1
|
||||
}
|
||||
return expRecur(n-1) + expRecur(n-1) + 1
|
||||
}
|
||||
|
||||
/* Logarithmic order (loop implementation) */
|
||||
func logarithmic(n int) int {
|
||||
count := 0
|
||||
for n > 1 {
|
||||
n = n / 2
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/* Logarithmic order (recursive implementation) */
|
||||
func logRecur(n int) int {
|
||||
if n <= 1 {
|
||||
return 0
|
||||
}
|
||||
return logRecur(n/2) + 1
|
||||
}
|
||||
|
||||
/* Linearithmic order */
|
||||
func linearLogRecur(n int) int {
|
||||
if n <= 1 {
|
||||
return 1
|
||||
}
|
||||
count := linearLogRecur(n/2) + linearLogRecur(n/2)
|
||||
for i := 0; i < n; i++ {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/* Factorial order (recursive implementation) */
|
||||
func factorialRecur(n int) int {
|
||||
if n == 0 {
|
||||
return 1
|
||||
}
|
||||
count := 0
|
||||
// Split from 1 into n
|
||||
for i := 0; i < n; i++ {
|
||||
count += factorialRecur(n - 1)
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// File: time_complexity_test.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTimeComplexity(t *testing.T) {
|
||||
n := 8
|
||||
fmt.Println("Input data size n =", n)
|
||||
|
||||
count := constant(n)
|
||||
fmt.Println("Number of constant-order operations =", count)
|
||||
|
||||
count = linear(n)
|
||||
fmt.Println("Number of linear-order operations =", count)
|
||||
count = arrayTraversal(make([]int, n))
|
||||
fmt.Println("Number of linear-order (array traversal) operations =", count)
|
||||
|
||||
count = quadratic(n)
|
||||
fmt.Println("Number of quadratic-order operations =", count)
|
||||
nums := make([]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
nums[i] = n - i
|
||||
}
|
||||
count = bubbleSort(nums)
|
||||
fmt.Println("Number of quadratic-order (bubble sort) operations =", count)
|
||||
|
||||
count = exponential(n)
|
||||
fmt.Println("Number of exponential-order (loop implementation) operations =", count)
|
||||
count = expRecur(n)
|
||||
fmt.Println("Number of exponential-order (recursive implementation) operations =", count)
|
||||
|
||||
count = logarithmic(n)
|
||||
fmt.Println("Number of logarithmic-order (loop implementation) operations =", count)
|
||||
count = logRecur(n)
|
||||
fmt.Println("Number of logarithmic-order (recursive implementation) operations =", count)
|
||||
|
||||
count = linearLogRecur(n)
|
||||
fmt.Println("Number of linearithmic-order (recursive implementation) operations =", count)
|
||||
|
||||
count = factorialRecur(n)
|
||||
fmt.Println("Number of factorial-order (recursive implementation) operations =", count)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// File: worst_best_time_complexity.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
func randomNumbers(n int) []int {
|
||||
nums := make([]int, n)
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
for i := 0; i < n; i++ {
|
||||
nums[i] = i + 1
|
||||
}
|
||||
// Randomly shuffle array elements
|
||||
rand.Shuffle(len(nums), func(i, j int) {
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
})
|
||||
return nums
|
||||
}
|
||||
|
||||
/* Find the index of number 1 in array nums */
|
||||
func findOne(nums []int) int {
|
||||
for i := 0; i < len(nums); 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
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// File: worst_best_time_complexity_test.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWorstBestTimeComplexity(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
n := 100
|
||||
nums := randomNumbers(n)
|
||||
index := findOne(nums)
|
||||
fmt.Println("\nAfter shuffling array [ 1, 2, ..., n ] =", nums)
|
||||
fmt.Println("Index of number 1 is", index)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user