mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 11:50: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,79 @@
|
||||
=begin
|
||||
File: iteration.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com), Cy (9738314@gmail.com)
|
||||
=end
|
||||
|
||||
### for loop ###
|
||||
def for_loop(n)
|
||||
res = 0
|
||||
|
||||
# Sum 1, 2, ..., n-1, n
|
||||
for i in 1..n
|
||||
res += i
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
|
||||
### while loop ###
|
||||
def while_loop(n)
|
||||
res = 0
|
||||
i = 1 # Initialize condition variable
|
||||
|
||||
# Sum 1, 2, ..., n-1, n
|
||||
while i <= n
|
||||
res += i
|
||||
i += 1 # Update condition variable
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
|
||||
### while loop (two updates) ###
|
||||
def while_loop_ii(n)
|
||||
res = 0
|
||||
i = 1 # Initialize condition variable
|
||||
|
||||
# Sum 1, 4, 10, ...
|
||||
while i <= n
|
||||
res += i
|
||||
# Update condition variable
|
||||
i += 1
|
||||
i *= 2
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
|
||||
### Nested for loop ###
|
||||
def nested_for_loop(n)
|
||||
res = ""
|
||||
|
||||
# Loop i = 1, 2, ..., n-1, n
|
||||
for i in 1..n
|
||||
# Loop j = 1, 2, ..., n-1, n
|
||||
for j in 1..n
|
||||
res += "(#{i}, #{j}), "
|
||||
end
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
res = for_loop(n)
|
||||
puts "\nFor loop sum result res = #{res}"
|
||||
|
||||
res = while_loop(n)
|
||||
puts "\nWhile loop sum result res = #{res}"
|
||||
|
||||
res = while_loop_ii(n)
|
||||
puts "\nWhile loop (two updates) sum result res = #{res}"
|
||||
|
||||
res = nested_for_loop(n)
|
||||
puts "\nNested for loop traversal result #{res}"
|
||||
end
|
||||
@@ -0,0 +1,70 @@
|
||||
=begin
|
||||
File: recursion.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Recursion ###
|
||||
def recur(n)
|
||||
# Termination condition
|
||||
return 1 if n == 1
|
||||
# Recurse: recursive call
|
||||
res = recur(n - 1)
|
||||
# Return: return result
|
||||
n + res
|
||||
end
|
||||
|
||||
### Use iteration to simulate recursion ###
|
||||
def for_loop_recur(n)
|
||||
# Use an explicit stack to simulate the system call stack
|
||||
stack = []
|
||||
res = 0
|
||||
|
||||
# Recurse: recursive call
|
||||
for i in n.downto(0)
|
||||
# Simulate "recurse" with "push"
|
||||
stack << i
|
||||
end
|
||||
# Return: return result
|
||||
while !stack.empty?
|
||||
res += stack.pop
|
||||
end
|
||||
|
||||
# res = 1+2+3+...+n
|
||||
res
|
||||
end
|
||||
|
||||
### Tail recursion ###
|
||||
def tail_recur(n, res)
|
||||
# Termination condition
|
||||
return res if n == 0
|
||||
# Tail recursive call
|
||||
tail_recur(n - 1, res + n)
|
||||
end
|
||||
|
||||
### Fibonacci sequence: recursion ###
|
||||
def fib(n)
|
||||
# Termination condition f(1) = 0, f(2) = 1
|
||||
return n - 1 if n == 1 || n == 2
|
||||
# Recursive call f(n) = f(n-1) + f(n-2)
|
||||
res = fib(n - 1) + fib(n - 2)
|
||||
# Return result f(n)
|
||||
res
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
res = recur(n)
|
||||
puts "\nRecursion sum result res = #{res}"
|
||||
|
||||
res = for_loop_recur(n)
|
||||
puts "\nUsing iteration to simulate recursion sum result res = #{res}"
|
||||
|
||||
res = tail_recur(n, 0)
|
||||
puts "\nTail recursion sum result res = #{res}"
|
||||
|
||||
res = fib(n)
|
||||
puts "\nThe #{n}th Fibonacci number is #{res}"
|
||||
end
|
||||
@@ -0,0 +1,92 @@
|
||||
=begin
|
||||
File: space_complexity.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative '../utils/list_node'
|
||||
require_relative '../utils/tree_node'
|
||||
require_relative '../utils/print_util'
|
||||
|
||||
### Function ###
|
||||
def function
|
||||
# Perform some operations
|
||||
0
|
||||
end
|
||||
|
||||
### Constant time ###
|
||||
def constant(n)
|
||||
# Constants, variables, objects occupy O(1) space
|
||||
a = 0
|
||||
nums = [0] * 10000
|
||||
node = ListNode.new
|
||||
|
||||
# Variables in the loop occupy O(1) space
|
||||
(0...n).each { c = 0 }
|
||||
# Functions in the loop occupy O(1) space
|
||||
(0...n).each { function }
|
||||
end
|
||||
|
||||
### Linear time ###
|
||||
def linear(n)
|
||||
# A list of length n occupies O(n) space
|
||||
nums = Array.new(n, 0)
|
||||
|
||||
# A hash table of length n occupies O(n) space
|
||||
hmap = {}
|
||||
for i in 0...n
|
||||
hmap[i] = i.to_s
|
||||
end
|
||||
end
|
||||
|
||||
### Linear space (recursive) ###
|
||||
def linear_recur(n)
|
||||
puts "Recursion n = #{n}"
|
||||
return if n == 1
|
||||
linear_recur(n - 1)
|
||||
end
|
||||
|
||||
### Quadratic time ###
|
||||
def quadratic(n)
|
||||
# 2D list uses O(n^2) space
|
||||
Array.new(n) { Array.new(n, 0) }
|
||||
end
|
||||
|
||||
### Quadratic space (recursive) ###
|
||||
def quadratic_recur(n)
|
||||
return 0 unless n > 0
|
||||
|
||||
# Array nums has length n, n-1, ..., 2, 1
|
||||
nums = Array.new(n, 0)
|
||||
quadratic_recur(n - 1)
|
||||
end
|
||||
|
||||
### Exponential space (build full binary tree) ###
|
||||
def build_tree(n)
|
||||
return if n == 0
|
||||
|
||||
TreeNode.new.tap do |root|
|
||||
root.left = build_tree(n - 1)
|
||||
root.right = build_tree(n - 1)
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
# Constant order
|
||||
constant(n)
|
||||
|
||||
# Linear order
|
||||
linear(n)
|
||||
linear_recur(n)
|
||||
|
||||
# Exponential order
|
||||
quadratic(n)
|
||||
quadratic_recur(n)
|
||||
|
||||
# Exponential order
|
||||
root = build_tree(n)
|
||||
print_tree(root)
|
||||
end
|
||||
@@ -0,0 +1,165 @@
|
||||
=begin
|
||||
File: time_complexity.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Constant time ###
|
||||
def constant(n)
|
||||
count = 0
|
||||
size = 100000
|
||||
|
||||
(0...size).each { count += 1 }
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Linear time ###
|
||||
def linear(n)
|
||||
count = 0
|
||||
(0...n).each { count += 1 }
|
||||
count
|
||||
end
|
||||
|
||||
### Linear time (array traversal) ###
|
||||
def array_traversal(nums)
|
||||
count = 0
|
||||
|
||||
# Number of iterations is proportional to the array length
|
||||
for num in nums
|
||||
count += 1
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Quadratic time ###
|
||||
def quadratic(n)
|
||||
count = 0
|
||||
|
||||
# Number of iterations is quadratically related to the data size n
|
||||
for i in 0...n
|
||||
for j in 0...n
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Quadratic time (bubble sort) ###
|
||||
def bubble_sort(nums)
|
||||
count = 0 # Counter
|
||||
|
||||
# Outer loop: unsorted range is [0, i]
|
||||
for i in (nums.length - 1).downto(0)
|
||||
# Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for j in 0...i
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Exponential time (iterative) ###
|
||||
def exponential(n)
|
||||
count, base = 0, 1
|
||||
|
||||
# Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
(0...n).each do
|
||||
(0...base).each { count += 1 }
|
||||
base *= 2
|
||||
end
|
||||
|
||||
# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
count
|
||||
end
|
||||
|
||||
### Exponential time (recursive) ###
|
||||
def exp_recur(n)
|
||||
return 1 if n == 1
|
||||
exp_recur(n - 1) + exp_recur(n - 1) + 1
|
||||
end
|
||||
|
||||
### Logarithmic time (iterative) ###
|
||||
def logarithmic(n)
|
||||
count = 0
|
||||
|
||||
while n > 1
|
||||
n /= 2
|
||||
count += 1
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Logarithmic time (recursive) ###
|
||||
def log_recur(n)
|
||||
return 0 unless n > 1
|
||||
log_recur(n / 2) + 1
|
||||
end
|
||||
|
||||
### Linearithmic time ###
|
||||
def linear_log_recur(n)
|
||||
return 1 unless n > 1
|
||||
|
||||
count = linear_log_recur(n / 2) + linear_log_recur(n / 2)
|
||||
(0...n).each { count += 1 }
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Factorial time (recursive) ###
|
||||
def factorial_recur(n)
|
||||
return 1 if n == 0
|
||||
|
||||
count = 0
|
||||
# Split from 1 into n
|
||||
(0...n).each { count += factorial_recur(n - 1) }
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# You can modify n to run and observe the trend of the number of operations for various complexities
|
||||
n = 8
|
||||
puts "Input data size n = #{n}"
|
||||
|
||||
count = constant(n)
|
||||
puts "Constant-time operations count = #{count}"
|
||||
|
||||
count = linear(n)
|
||||
puts "Linear-time operations count = #{count}"
|
||||
count = array_traversal(Array.new(n, 0))
|
||||
puts "Linear-time (array traversal) operations count = #{count}"
|
||||
|
||||
count = quadratic(n)
|
||||
puts "Quadratic-time operations count = #{count}"
|
||||
nums = Array.new(n) { |i| n - i } # [n, n-1, ..., 2, 1]
|
||||
count = bubble_sort(nums)
|
||||
puts "Quadratic-time (bubble sort) operations count = #{count}"
|
||||
|
||||
count = exponential(n)
|
||||
puts "Exponential-time (iterative) operations count = #{count}"
|
||||
count = exp_recur(n)
|
||||
puts "Exponential-time (recursive) operations count = #{count}"
|
||||
|
||||
count = logarithmic(n)
|
||||
puts "Logarithmic-time (iterative) operations count = #{count}"
|
||||
count = log_recur(n)
|
||||
puts "Logarithmic-time (recursive) operations count = #{count}"
|
||||
|
||||
count = linear_log_recur(n)
|
||||
puts "Linearithmic-time (recursive) operations count = #{count}"
|
||||
|
||||
count = factorial_recur(n)
|
||||
puts "Factorial-time (recursive) operations count = #{count}"
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
=begin
|
||||
File: worst_best_time_complexity.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Generate array with elements: 1, 2, ..., n, shuffled ###
|
||||
def random_numbers(n)
|
||||
# Generate array nums =: 1, 2, 3, ..., n
|
||||
nums = Array.new(n) { |i| i + 1 }
|
||||
# Randomly shuffle array elements
|
||||
nums.shuffle!
|
||||
end
|
||||
|
||||
### Find index of number 1 in array nums ###
|
||||
def find_one(nums)
|
||||
for i in 0...nums.length
|
||||
# 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
|
||||
return i if nums[i] == 1
|
||||
end
|
||||
|
||||
-1
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
for i in 0...10
|
||||
n = 100
|
||||
nums = random_numbers(n)
|
||||
index = find_one(nums)
|
||||
puts "\nArray [ 1, 2, ..., n ] after shuffling = #{nums}"
|
||||
puts "Index of number 1 is #{index}"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user