mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-30 17:44:26 +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:
@@ -8,39 +8,39 @@ import random
|
||||
|
||||
|
||||
def random_access(nums: list[int]) -> int:
|
||||
"""Random access to elements"""
|
||||
"""Random access to element"""
|
||||
# Randomly select a number from the interval [0, len(nums)-1]
|
||||
random_index = random.randint(0, len(nums) - 1)
|
||||
# Retrieve and return a random element
|
||||
# Retrieve and return the random element
|
||||
random_num = nums[random_index]
|
||||
return random_num
|
||||
|
||||
|
||||
# Note that Python's list is a dynamic array that can be extended
|
||||
# For ease of learning, this function treats the list as a static array
|
||||
# Please note that Python's list is a dynamic array and can be extended directly
|
||||
# For learning purposes, this function treats the list as an array with immutable length
|
||||
def extend(nums: list[int], enlarge: int) -> list[int]:
|
||||
"""Extend array length"""
|
||||
# Initialize an extended length array
|
||||
# Initialize an array with extended length
|
||||
res = [0] * (len(nums) + enlarge)
|
||||
# Copy all elements from the original array to the new array
|
||||
for i in range(len(nums)):
|
||||
res[i] = nums[i]
|
||||
# Return the new array after expansion
|
||||
# Return the extended new array
|
||||
return res
|
||||
|
||||
|
||||
def insert(nums: list[int], num: int, index: int):
|
||||
"""Insert element num at `index`"""
|
||||
# Move all elements after `index` one position backward
|
||||
"""Insert element num at index index in the array"""
|
||||
# Move all elements at and after index index backward by one position
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
nums[i] = nums[i - 1]
|
||||
# Assign num to the element at index
|
||||
# Assign num to the element at index index
|
||||
nums[index] = num
|
||||
|
||||
|
||||
def remove(nums: list[int], index: int):
|
||||
"""Remove the element at `index`"""
|
||||
# Move all elements after `index` one position forward
|
||||
"""Remove the element at index index"""
|
||||
# Move all elements after index index forward by one position
|
||||
for i in range(index, len(nums) - 1):
|
||||
nums[i] = nums[i + 1]
|
||||
|
||||
@@ -51,17 +51,17 @@ def traverse(nums: list[int]):
|
||||
# Traverse array by index
|
||||
for i in range(len(nums)):
|
||||
count += nums[i]
|
||||
# Traverse array elements
|
||||
# Direct traversal of array elements
|
||||
for num in nums:
|
||||
count += num
|
||||
# Traverse both data index and elements
|
||||
# Traverse simultaneously data index and elements
|
||||
for i, num in enumerate(nums):
|
||||
count += nums[i]
|
||||
count += num
|
||||
|
||||
|
||||
def find(nums: list[int], target: int) -> int:
|
||||
"""Search for a specified element in the array"""
|
||||
"""Find the specified element in the array"""
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target:
|
||||
return i
|
||||
@@ -70,7 +70,7 @@ def find(nums: list[int], target: int) -> int:
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize an array
|
||||
# Initialize array
|
||||
arr = [0] * 5
|
||||
print("Array arr =", arr)
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
@@ -78,23 +78,23 @@ if __name__ == "__main__":
|
||||
|
||||
# Random access
|
||||
random_num: int = random_access(nums)
|
||||
print("Retrieve a random element in nums", random_num)
|
||||
print("Get random element from nums", random_num)
|
||||
|
||||
# Length extension
|
||||
nums: list[int] = extend(nums, 3)
|
||||
print("Extend the array length to 8, resulting in nums =", nums)
|
||||
print("Extend the array length to 8, get nums =", nums)
|
||||
|
||||
# Insert element
|
||||
insert(nums, 6, 3)
|
||||
print("Insert number 6 at index 3, resulting in nums =", nums)
|
||||
print("Insert number 6 at index 3, get nums =", nums)
|
||||
|
||||
# Remove element
|
||||
remove(nums, 2)
|
||||
print("Remove the element at index 2, resulting in nums =", nums)
|
||||
print("Remove the element at index 2, get nums =", nums)
|
||||
|
||||
# Traverse array
|
||||
traverse(nums)
|
||||
|
||||
# Search for elements
|
||||
# Find element
|
||||
index: int = find(nums, 3)
|
||||
print("Search for element 3 in nums, resulting in index =", index)
|
||||
print("Search for element 3 in nums, get index =", index)
|
||||
|
||||
@@ -29,7 +29,7 @@ def remove(n0: ListNode):
|
||||
|
||||
|
||||
def access(head: ListNode, index: int) -> ListNode | None:
|
||||
"""Access the node at `index` in the linked list"""
|
||||
"""Access the node at index index in the linked list"""
|
||||
for _ in range(index):
|
||||
if not head:
|
||||
return None
|
||||
@@ -38,7 +38,7 @@ def access(head: ListNode, index: int) -> ListNode | None:
|
||||
|
||||
|
||||
def find(head: ListNode, target: int) -> int:
|
||||
"""Search for the first node with value target in the linked list"""
|
||||
"""Find the first node with value target in the linked list"""
|
||||
index = 0
|
||||
while head:
|
||||
if head.val == target:
|
||||
@@ -68,18 +68,18 @@ if __name__ == "__main__":
|
||||
# Insert node
|
||||
p = ListNode(0)
|
||||
insert(n0, p)
|
||||
print("Linked list after inserting the node is")
|
||||
print("The linked list after inserting a node is")
|
||||
print_linked_list(n0)
|
||||
|
||||
# Remove node
|
||||
remove(n0)
|
||||
print("Linked list after removing the node is")
|
||||
print("The linked list after removing a node is")
|
||||
print_linked_list(n0)
|
||||
|
||||
# Access node
|
||||
node: ListNode = access(n0, 3)
|
||||
print("The value of the node at index 3 in the linked list = {}".format(node.val))
|
||||
|
||||
# Search node
|
||||
# Find node
|
||||
index: int = find(n0, 2)
|
||||
print("The index of the node with value 2 in the linked list = {}".format(index))
|
||||
|
||||
@@ -12,45 +12,45 @@ if __name__ == "__main__":
|
||||
|
||||
# Access element
|
||||
x: int = nums[1]
|
||||
print("\nAccess the element at index 1, resulting in x =", x)
|
||||
print("\nAccess the element at index 1, get x =", x)
|
||||
|
||||
# Update element
|
||||
nums[1] = 0
|
||||
print("\nUpdate the element at index 1 to 0, resulting in nums =", nums)
|
||||
print("\nUpdate the element at index 1 to 0, get nums =", nums)
|
||||
|
||||
# Clear list
|
||||
nums.clear()
|
||||
print("\nAfter clearing the list, nums =", nums)
|
||||
print("\nAfter clearing the list nums =", nums)
|
||||
|
||||
# Add element at the end
|
||||
# Add elements at the end
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
print("\nAfter adding the element, nums =", nums)
|
||||
print("\nAfter adding elements nums =", nums)
|
||||
|
||||
# Insert element in the middle
|
||||
nums.insert(3, 6)
|
||||
print("\nInsert number 6 at index 3, resulting in nums =", nums)
|
||||
print("\nInsert number 6 at index 3, get nums =", nums)
|
||||
|
||||
# Remove element
|
||||
nums.pop(3)
|
||||
print("\nRemove the element at index 3, resulting in nums =", nums)
|
||||
print("\nRemove the element at index 3, get nums =", nums)
|
||||
|
||||
# Traverse the list by index
|
||||
# Traverse list by index
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
count += nums[i]
|
||||
# Traverse the list elements
|
||||
# Direct traversal of list elements
|
||||
for num in nums:
|
||||
count += num
|
||||
|
||||
# Concatenate two lists
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
print("\nConcatenate list nums1 to nums, resulting in nums =", nums)
|
||||
print("\nConcatenate list nums1 to nums, get nums =", nums)
|
||||
|
||||
# Sort list
|
||||
nums.sort()
|
||||
print("\nAfter sorting the list, nums =", nums)
|
||||
print("\nAfter sorting the list nums =", nums)
|
||||
|
||||
@@ -13,7 +13,7 @@ class MyList:
|
||||
self._capacity: int = 10 # List capacity
|
||||
self._arr: list[int] = [0] * self._capacity # Array (stores list elements)
|
||||
self._size: int = 0 # List length (current number of elements)
|
||||
self._extend_ratio: int = 2 # Multiple for each list expansion
|
||||
self._extend_ratio: int = 2 # Multiple by which the list capacity is extended each time
|
||||
|
||||
def size(self) -> int:
|
||||
"""Get list length (current number of elements)"""
|
||||
@@ -38,7 +38,7 @@ class MyList:
|
||||
|
||||
def add(self, num: int):
|
||||
"""Add element at the end"""
|
||||
# When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
# When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if self.size() == self.capacity():
|
||||
self.extend_capacity()
|
||||
self._arr[self._size] = num
|
||||
@@ -48,10 +48,10 @@ class MyList:
|
||||
"""Insert element in the middle"""
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("Index out of bounds")
|
||||
# When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
# When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if self._size == self.capacity():
|
||||
self.extend_capacity()
|
||||
# Move all elements after `index` one position backward
|
||||
# Move all elements at and after index index backward by one position
|
||||
for j in range(self._size - 1, index - 1, -1):
|
||||
self._arr[j + 1] = self._arr[j]
|
||||
self._arr[index] = num
|
||||
@@ -63,7 +63,7 @@ class MyList:
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("Index out of bounds")
|
||||
num = self._arr[index]
|
||||
# Move all elements after `index` one position forward
|
||||
# Move all elements after index index forward by one position
|
||||
for j in range(index, self._size - 1):
|
||||
self._arr[j] = self._arr[j + 1]
|
||||
# Update the number of elements
|
||||
@@ -72,14 +72,14 @@ class MyList:
|
||||
return num
|
||||
|
||||
def extend_capacity(self):
|
||||
"""Extend list"""
|
||||
# Create a new array of _extend_ratio times the length of the original array and copy the original array to the new array
|
||||
"""Extend list capacity"""
|
||||
# Create a new array with length _extend_ratio times the original array, and copy the original array to the new array
|
||||
self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)
|
||||
# Update list capacity
|
||||
self._capacity = len(self._arr)
|
||||
|
||||
def to_array(self) -> list[int]:
|
||||
"""Return a list of valid lengths"""
|
||||
"""Return list with valid length"""
|
||||
return self._arr[: self._size]
|
||||
|
||||
|
||||
@@ -87,32 +87,32 @@ class MyList:
|
||||
if __name__ == "__main__":
|
||||
# Initialize list
|
||||
nums = MyList()
|
||||
# Add element at the end
|
||||
# Add elements at the end
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
print(f"List nums = {nums.to_array()} ,capacity = {nums.capacity()} ,length = {nums.size()}")
|
||||
print(f"List nums = {nums.to_array()}, capacity = {nums.capacity()}, length = {nums.size()}")
|
||||
|
||||
# Insert element in the middle
|
||||
nums.insert(6, index=3)
|
||||
print("Insert number 6 at index 3, resulting in nums =", nums.to_array())
|
||||
print("Insert number 6 at index 3, get nums =", nums.to_array())
|
||||
|
||||
# Remove element
|
||||
nums.remove(3)
|
||||
print("Remove the element at index 3, resulting in nums =", nums.to_array())
|
||||
print("Remove the element at index 3, get nums =", nums.to_array())
|
||||
|
||||
# Access element
|
||||
num = nums.get(1)
|
||||
print("Access the element at index 1, resulting in num =", num)
|
||||
print("Access the element at index 1, get num =", num)
|
||||
|
||||
# Update element
|
||||
nums.set(0, 1)
|
||||
print("Update the element at index 1 to 0, resulting in nums =", nums.to_array())
|
||||
print("Update the element at index 1 to 0, get nums =", nums.to_array())
|
||||
|
||||
# Test expansion mechanism
|
||||
# Test extension mechanism
|
||||
for i in range(10):
|
||||
# At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism at this time
|
||||
# At i = 5, the list length will exceed the list capacity, triggering the extension mechanism at this point
|
||||
nums.add(i)
|
||||
print(f"After expansion, the list {nums.to_array()} ,capacity = {nums.capacity()} ,length = {nums.size()}")
|
||||
print(f"List after extension {nums.to_array()}, capacity = {nums.capacity()}, length = {nums.size()}")
|
||||
|
||||
Reference in New Issue
Block a user