mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +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:
@@ -8,7 +8,7 @@ Author: krahets (krahets@163.com)
|
||||
def for_loop(n: int) -> int:
|
||||
"""for loop"""
|
||||
res = 0
|
||||
# Loop sum 1, 2, ..., n-1, n
|
||||
# Sum 1, 2, ..., n-1, n
|
||||
for i in range(1, n + 1):
|
||||
res += i
|
||||
return res
|
||||
@@ -18,7 +18,7 @@ def while_loop(n: int) -> int:
|
||||
"""while loop"""
|
||||
res = 0
|
||||
i = 1 # Initialize condition variable
|
||||
# Loop sum 1, 2, ..., n-1, n
|
||||
# Sum 1, 2, ..., n-1, n
|
||||
while i <= n:
|
||||
res += i
|
||||
i += 1 # Update condition variable
|
||||
@@ -29,7 +29,7 @@ def while_loop_ii(n: int) -> int:
|
||||
"""while loop (two updates)"""
|
||||
res = 0
|
||||
i = 1 # Initialize condition variable
|
||||
# Loop sum 1, 4, 10, ...
|
||||
# Sum 1, 4, 10, ...
|
||||
while i <= n:
|
||||
res += i
|
||||
# Update condition variable
|
||||
@@ -39,7 +39,7 @@ def while_loop_ii(n: int) -> int:
|
||||
|
||||
|
||||
def nested_for_loop(n: int) -> str:
|
||||
"""Double for loop"""
|
||||
"""Nested for loop"""
|
||||
res = ""
|
||||
# Loop i = 1, 2, ..., n-1, n
|
||||
for i in range(1, n + 1):
|
||||
@@ -53,13 +53,13 @@ def nested_for_loop(n: int) -> str:
|
||||
if __name__ == "__main__":
|
||||
n = 5
|
||||
res = for_loop(n)
|
||||
print(f"\nfor loop sum result res = {res}")
|
||||
print(f"\nSum result of for loop res = {res}")
|
||||
|
||||
res = while_loop(n)
|
||||
print(f"\nwhile loop sum result res = {res}")
|
||||
print(f"\nSum result of while loop res = {res}")
|
||||
|
||||
res = while_loop_ii(n)
|
||||
print(f"\nwhile loop (two updates) sum result res = {res}")
|
||||
print(f"\nSum result of while loop (two updates) res = {res}")
|
||||
|
||||
res = nested_for_loop(n)
|
||||
print(f"\nDouble for loop traversal result {res}")
|
||||
print(f"\nTraversal result of nested for loop {res}")
|
||||
|
||||
@@ -10,24 +10,24 @@ def recur(n: int) -> int:
|
||||
# Termination condition
|
||||
if n == 1:
|
||||
return 1
|
||||
# Recursive: recursive call
|
||||
# Recurse: recursive call
|
||||
res = recur(n - 1)
|
||||
# Return: return result
|
||||
return n + res
|
||||
|
||||
|
||||
def for_loop_recur(n: int) -> int:
|
||||
"""Simulate recursion with iteration"""
|
||||
"""Simulate recursion using iteration"""
|
||||
# Use an explicit stack to simulate the system call stack
|
||||
stack = []
|
||||
res = 0
|
||||
# Recursive: recursive call
|
||||
# Recurse: recursive call
|
||||
for i in range(n, 0, -1):
|
||||
# Simulate "recursive" by "pushing onto the stack"
|
||||
# Simulate "recurse" with "push"
|
||||
stack.append(i)
|
||||
# Return: return result
|
||||
while stack:
|
||||
# Simulate "return" by "popping from the stack"
|
||||
# Simulate "return" with "pop"
|
||||
res += stack.pop()
|
||||
# res = 1+2+3+...+n
|
||||
return res
|
||||
@@ -43,7 +43,7 @@ def tail_recur(n, res):
|
||||
|
||||
|
||||
def fib(n: int) -> int:
|
||||
"""Fibonacci sequence: Recursion"""
|
||||
"""Fibonacci sequence: recursion"""
|
||||
# Termination condition f(1) = 0, f(2) = 1
|
||||
if n == 1 or n == 2:
|
||||
return n - 1
|
||||
@@ -57,13 +57,13 @@ def fib(n: int) -> int:
|
||||
if __name__ == "__main__":
|
||||
n = 5
|
||||
res = recur(n)
|
||||
print(f"\nRecursive function sum result res = {res}")
|
||||
print(f"\nSum result of recursive function res = {res}")
|
||||
|
||||
res = for_loop_recur(n)
|
||||
print(f"\nSimulate recursion with iteration sum result res = {res}")
|
||||
print(f"\nSum result of simulating recursion using iteration res = {res}")
|
||||
|
||||
res = tail_recur(n, 0)
|
||||
print(f"\nTail recursive function sum result res = {res}")
|
||||
print(f"\nSum result of tail recursive function res = {res}")
|
||||
|
||||
res = fib(n)
|
||||
print(f"\nThe n th term of the Fibonacci sequence is {res}")
|
||||
print(f"\nThe {n}th term of the Fibonacci sequence is {res}")
|
||||
|
||||
@@ -18,21 +18,21 @@ def function() -> int:
|
||||
|
||||
|
||||
def constant(n: int):
|
||||
"""Constant complexity"""
|
||||
"""Constant order"""
|
||||
# Constants, variables, objects occupy O(1) space
|
||||
a = 0
|
||||
nums = [0] * 10000
|
||||
node = ListNode(0)
|
||||
# Variables in a loop occupy O(1) space
|
||||
# Variables in the loop occupy O(1) space
|
||||
for _ in range(n):
|
||||
c = 0
|
||||
# Functions in a loop occupy O(1) space
|
||||
# Functions in the loop occupy O(1) space
|
||||
for _ in range(n):
|
||||
function()
|
||||
|
||||
|
||||
def linear(n: int):
|
||||
"""Linear complexity"""
|
||||
"""Linear order"""
|
||||
# A list of length n occupies O(n) space
|
||||
nums = [0] * n
|
||||
# A hash table of length n occupies O(n) space
|
||||
@@ -42,30 +42,30 @@ def linear(n: int):
|
||||
|
||||
|
||||
def linear_recur(n: int):
|
||||
"""Linear complexity (recursive implementation)"""
|
||||
print("Recursive n =", n)
|
||||
"""Linear order (recursive implementation)"""
|
||||
print("Recursion n =", n)
|
||||
if n == 1:
|
||||
return
|
||||
linear_recur(n - 1)
|
||||
|
||||
|
||||
def quadratic(n: int):
|
||||
"""Quadratic complexity"""
|
||||
# A two-dimensional list occupies O(n^2) space
|
||||
"""Quadratic order"""
|
||||
# A 2D list occupies O(n^2) space
|
||||
num_matrix = [[0] * n for _ in range(n)]
|
||||
|
||||
|
||||
def quadratic_recur(n: int) -> int:
|
||||
"""Quadratic complexity (recursive implementation)"""
|
||||
"""Quadratic order (recursive implementation)"""
|
||||
if n <= 0:
|
||||
return 0
|
||||
# Array nums length = n, n-1, ..., 2, 1
|
||||
# Array nums length is n, n-1, ..., 2, 1
|
||||
nums = [0] * n
|
||||
return quadratic_recur(n - 1)
|
||||
|
||||
|
||||
def build_tree(n: int) -> TreeNode | None:
|
||||
"""Exponential complexity (building a full binary tree)"""
|
||||
"""Exponential order (build full binary tree)"""
|
||||
if n == 0:
|
||||
return None
|
||||
root = TreeNode(0)
|
||||
@@ -77,14 +77,14 @@ def build_tree(n: int) -> TreeNode | None:
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
n = 5
|
||||
# Constant complexity
|
||||
# Constant order
|
||||
constant(n)
|
||||
# Linear complexity
|
||||
# Linear order
|
||||
linear(n)
|
||||
linear_recur(n)
|
||||
# Quadratic complexity
|
||||
# Quadratic order
|
||||
quadratic(n)
|
||||
quadratic_recur(n)
|
||||
# Exponential complexity
|
||||
# Exponential order
|
||||
root = build_tree(n)
|
||||
print_tree(root)
|
||||
|
||||
@@ -6,7 +6,7 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
def constant(n: int) -> int:
|
||||
"""Constant complexity"""
|
||||
"""Constant order"""
|
||||
count = 0
|
||||
size = 100000
|
||||
for _ in range(size):
|
||||
@@ -15,7 +15,7 @@ def constant(n: int) -> int:
|
||||
|
||||
|
||||
def linear(n: int) -> int:
|
||||
"""Linear complexity"""
|
||||
"""Linear order"""
|
||||
count = 0
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
@@ -23,18 +23,18 @@ def linear(n: int) -> int:
|
||||
|
||||
|
||||
def array_traversal(nums: list[int]) -> int:
|
||||
"""Linear complexity (traversing an array)"""
|
||||
"""Linear order (traversing array)"""
|
||||
count = 0
|
||||
# Loop count is proportional to the length of the array
|
||||
# Number of iterations is proportional to the array length
|
||||
for num in nums:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def quadratic(n: int) -> int:
|
||||
"""Quadratic complexity"""
|
||||
"""Quadratic order"""
|
||||
count = 0
|
||||
# Loop count is squared in relation to the data size n
|
||||
# Number of iterations is quadratically related to the data size n
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
count += 1
|
||||
@@ -42,26 +42,26 @@ def quadratic(n: int) -> int:
|
||||
|
||||
|
||||
def bubble_sort(nums: list[int]) -> int:
|
||||
"""Quadratic complexity (bubble sort)"""
|
||||
"""Quadratic order (bubble sort)"""
|
||||
count = 0 # Counter
|
||||
# Outer loop: unsorted range is [0, i]
|
||||
for i in range(len(nums) - 1, 0, -1):
|
||||
# Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
# Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# Swap nums[j] and nums[j + 1]
|
||||
tmp: int = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = tmp
|
||||
count += 3 # Element swap includes 3 individual operations
|
||||
count += 3 # Element swap includes 3 unit operations
|
||||
return count
|
||||
|
||||
|
||||
def exponential(n: int) -> int:
|
||||
"""Exponential complexity (loop implementation)"""
|
||||
"""Exponential order (loop implementation)"""
|
||||
count = 0
|
||||
base = 1
|
||||
# Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
# Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for _ in range(n):
|
||||
for _ in range(base):
|
||||
count += 1
|
||||
@@ -71,14 +71,14 @@ def exponential(n: int) -> int:
|
||||
|
||||
|
||||
def exp_recur(n: int) -> int:
|
||||
"""Exponential complexity (recursive implementation)"""
|
||||
"""Exponential order (recursive implementation)"""
|
||||
if n == 1:
|
||||
return 1
|
||||
return exp_recur(n - 1) + exp_recur(n - 1) + 1
|
||||
|
||||
|
||||
def logarithmic(n: int) -> int:
|
||||
"""Logarithmic complexity (loop implementation)"""
|
||||
"""Logarithmic order (loop implementation)"""
|
||||
count = 0
|
||||
while n > 1:
|
||||
n = n / 2
|
||||
@@ -87,28 +87,30 @@ def logarithmic(n: int) -> int:
|
||||
|
||||
|
||||
def log_recur(n: int) -> int:
|
||||
"""Logarithmic complexity (recursive implementation)"""
|
||||
"""Logarithmic order (recursive implementation)"""
|
||||
if n <= 1:
|
||||
return 0
|
||||
return log_recur(n / 2) + 1
|
||||
|
||||
|
||||
def linear_log_recur(n: int) -> int:
|
||||
"""Linear logarithmic complexity"""
|
||||
"""Linearithmic order"""
|
||||
if n <= 1:
|
||||
return 1
|
||||
count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)
|
||||
# Divide into two, the scale of subproblems is reduced by half
|
||||
count = linear_log_recur(n // 2) + linear_log_recur(n // 2)
|
||||
# Current subproblem contains n operations
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def factorial_recur(n: int) -> int:
|
||||
"""Factorial complexity (recursive implementation)"""
|
||||
"""Factorial order (recursive implementation)"""
|
||||
if n == 0:
|
||||
return 1
|
||||
count = 0
|
||||
# From 1 split into n
|
||||
# Split from 1 into n
|
||||
for _ in range(n):
|
||||
count += factorial_recur(n - 1)
|
||||
return count
|
||||
@@ -116,36 +118,36 @@ def factorial_recur(n: int) -> int:
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Can modify n to experience the trend of operation count changes under various complexities
|
||||
# You can modify n to run and observe the trend of the number of operations for various complexities
|
||||
n = 8
|
||||
print("Input data size n =", n)
|
||||
|
||||
count: int = constant(n)
|
||||
print("Constant complexity operation count =", count)
|
||||
count = constant(n)
|
||||
print("Number of operations of constant order =", count)
|
||||
|
||||
count: int = linear(n)
|
||||
print("Linear complexity operation count =", count)
|
||||
count: int = array_traversal([0] * n)
|
||||
print("Linear complexity (traversing an array) operation count =", count)
|
||||
count = linear(n)
|
||||
print("Number of operations of linear order =", count)
|
||||
count = array_traversal([0] * n)
|
||||
print("Number of operations of linear order (traversing array) =", count)
|
||||
|
||||
count: int = quadratic(n)
|
||||
print("Quadratic complexity operation count =", count)
|
||||
count = quadratic(n)
|
||||
print("Number of operations of quadratic order =", count)
|
||||
nums = [i for i in range(n, 0, -1)] # [n, n-1, ..., 2, 1]
|
||||
count: int = bubble_sort(nums)
|
||||
print("Quadratic complexity (bubble sort) operation count =", count)
|
||||
count = bubble_sort(nums)
|
||||
print("Number of operations of quadratic order (bubble sort) =", count)
|
||||
|
||||
count: int = exponential(n)
|
||||
print("Exponential complexity (loop implementation) operation count =", count)
|
||||
count: int = exp_recur(n)
|
||||
print("Exponential complexity (recursive implementation) operation count =", count)
|
||||
count = exponential(n)
|
||||
print("Number of operations of exponential order (loop implementation) =", count)
|
||||
count = exp_recur(n)
|
||||
print("Number of operations of exponential order (recursive implementation) =", count)
|
||||
|
||||
count: int = logarithmic(n)
|
||||
print("Logarithmic complexity (loop implementation) operation count =", count)
|
||||
count: int = log_recur(n)
|
||||
print("Logarithmic complexity (recursive implementation) operation count =", count)
|
||||
count = logarithmic(n)
|
||||
print("Number of operations of logarithmic order (loop implementation) =", count)
|
||||
count = log_recur(n)
|
||||
print("Number of operations of logarithmic order (recursive implementation) =", count)
|
||||
|
||||
count: int = linear_log_recur(n)
|
||||
print("Linear logarithmic complexity (recursive implementation) operation count =", count)
|
||||
count = linear_log_recur(n)
|
||||
print("Number of operations of linearithmic order (recursive implementation) =", count)
|
||||
|
||||
count: int = factorial_recur(n)
|
||||
print("Factorial complexity (recursive implementation) operation count =", count)
|
||||
count = factorial_recur(n)
|
||||
print("Number of operations of factorial order (recursive implementation) =", count)
|
||||
|
||||
@@ -8,7 +8,7 @@ import random
|
||||
|
||||
|
||||
def random_numbers(n: int) -> list[int]:
|
||||
"""Generate an array with elements: 1, 2, ..., n, order shuffled"""
|
||||
"""Generate an array with elements: 1, 2, ..., n, shuffled in order"""
|
||||
# Generate array nums =: 1, 2, 3, ..., n
|
||||
nums = [i for i in range(1, n + 1)]
|
||||
# Randomly shuffle array elements
|
||||
@@ -19,8 +19,8 @@ def random_numbers(n: int) -> list[int]:
|
||||
def find_one(nums: list[int]) -> int:
|
||||
"""Find the index of number 1 in array nums"""
|
||||
for i in range(len(nums)):
|
||||
# When element 1 is at the start of the array, achieve best time complexity O(1)
|
||||
# When element 1 is at the end of the array, achieve worst time complexity O(n)
|
||||
# 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
|
||||
@@ -32,5 +32,5 @@ if __name__ == "__main__":
|
||||
n = 100
|
||||
nums: list[int] = random_numbers(n)
|
||||
index: int = find_one(nums)
|
||||
print("\nThe array [ 1, 2, ..., n ] after being shuffled =", nums)
|
||||
print("Index of number 1 =", index)
|
||||
print("\nArray [ 1, 2, ..., n ] after being shuffled =", nums)
|
||||
print("The index of number 1 is", index)
|
||||
|
||||
Reference in New Issue
Block a user