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:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
@@ -6,36 +6,36 @@ Author: timi (xisunyy@163.com)
def binary_search(nums: list[int], target: int) -> int:
"""Binary search (double closed interval)"""
# Initialize double closed interval [0, n-1], i.e., i, j point to the first element and last element of the array respectively
"""Binary search (closed interval)"""
# Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
i, j = 0, len(nums) - 1
# Loop until the search interval is empty (when i > j, it is empty)
# Loop, exit when the search interval is empty (empty when i > j)
while i <= j:
# Theoretically, Python's numbers can be infinitely large (depending on memory size), so there is no need to consider large number overflow
m = i + (j - i) // 2 # Calculate midpoint index m
# In theory, Python numbers can be infinitely large (depending on memory size), no need to consider large number overflow
m = (i + j) // 2 # Calculate midpoint index m
if nums[m] < target:
i = m + 1 # This situation indicates that target is in the interval [m+1, j]
i = m + 1 # This means target is in the interval [m+1, j]
elif nums[m] > target:
j = m - 1 # This situation indicates that target is in the interval [i, m-1]
j = m - 1 # This means target is in the interval [i, m-1]
else:
return m # Found the target element, thus return its index
return -1 # Did not find the target element, thus return -1
return m # Found the target element, return its index
return -1 # Target element not found, return -1
def binary_search_lcro(nums: list[int], target: int) -> int:
"""Binary search (left closed right open interval)"""
# Initialize left closed right open interval [0, n), i.e., i, j point to the first element and the last element +1 of the array respectively
"""Binary search (left-closed right-open interval)"""
# Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
i, j = 0, len(nums)
# Loop until the search interval is empty (when i = j, it is empty)
# Loop, exit when the search interval is empty (empty when i = j)
while i < j:
m = i + (j - i) // 2 # Calculate midpoint index m
m = (i + j) // 2 # Calculate midpoint index m
if nums[m] < target:
i = m + 1 # This situation indicates that target is in the interval [m+1, j)
i = m + 1 # This means target is in the interval [m+1, j)
elif nums[m] > target:
j = m # This situation indicates that target is in the interval [i, m)
j = m # This means target is in the interval [i, m)
else:
return m # Found the target element, thus return its index
return -1 # Did not find the target element, thus return -1
return m # Found the target element, return its index
return -1 # Target element not found, return -1
"""Driver Code"""
@@ -43,10 +43,10 @@ if __name__ == "__main__":
target = 6
nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
# Binary search (double closed interval)
# Binary search (closed interval)
index = binary_search(nums, target)
print("Index of target element 6 =", index)
# Binary search (left closed right open interval)
# Binary search (left-closed right-open interval)
index = binary_search_lcro(nums, target)
print("Index of target element 6 =", index)
@@ -15,7 +15,7 @@ def binary_search_left_edge(nums: list[int], target: int) -> int:
"""Binary search for the leftmost target"""
# Equivalent to finding the insertion point of target
i = binary_search_insertion(nums, target)
# Did not find target, thus return -1
# Target not found, return -1
if i == len(nums) or nums[i] != target:
return -1
# Found target, return index i
@@ -28,7 +28,7 @@ def binary_search_right_edge(nums: list[int], target: int) -> int:
i = binary_search_insertion(nums, target + 1)
# j points to the rightmost target, i points to the first element greater than target
j = i - 1
# Did not find target, thus return -1
# Target not found, return -1
if j == -1 or nums[j] != target:
return -1
# Found target, return index j
@@ -41,9 +41,9 @@ if __name__ == "__main__":
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print(f"\nArray nums = {nums}")
# Binary search for left and right boundaries
# Binary search for left boundary and right boundary
for target in [6, 7]:
index = binary_search_left_edge(nums, target)
print(f"The index of the leftmost element {target} is {index}")
print(f"Index of the leftmost element {target} is {index}")
index = binary_search_right_edge(nums, target)
print(f"The index of the rightmost element {target} is {index}")
print(f"Index of the rightmost element {target} is {index}")
@@ -7,30 +7,30 @@ Author: krahets (krahets@163.com)
def binary_search_insertion_simple(nums: list[int], target: int) -> int:
"""Binary search for insertion point (no duplicate elements)"""
i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1]
i, j = 0, len(nums) - 1 # Initialize closed interval [0, n-1]
while i <= j:
m = i + (j - i) // 2 # Calculate midpoint index m
m = (i + j) // 2 # Calculate midpoint index m
if nums[m] < target:
i = m + 1 # Target is in interval [m+1, j]
i = m + 1 # target is in the interval [m+1, j]
elif nums[m] > target:
j = m - 1 # Target is in interval [i, m-1]
j = m - 1 # target is in the interval [i, m-1]
else:
return m # Found target, return insertion point m
# Did not find target, return insertion point i
# Target not found, return insertion point i
return i
def binary_search_insertion(nums: list[int], target: int) -> int:
"""Binary search for insertion point (with duplicate elements)"""
i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1]
i, j = 0, len(nums) - 1 # Initialize closed interval [0, n-1]
while i <= j:
m = i + (j - i) // 2 # Calculate midpoint index m
m = (i + j) // 2 # Calculate midpoint index m
if nums[m] < target:
i = m + 1 # Target is in interval [m+1, j]
i = m + 1 # target is in the interval [m+1, j]
elif nums[m] > target:
j = m - 1 # Target is in interval [i, m-1]
j = m - 1 # target is in the interval [i, m-1]
else:
j = m - 1 # First element less than target is in interval [i, m-1]
j = m - 1 # The first element less than target is in the interval [i, m-1]
# Return insertion point i
return i
@@ -43,7 +43,7 @@ if __name__ == "__main__":
# Binary search for insertion point
for target in [6, 9]:
index = binary_search_insertion_simple(nums, target)
print(f"Element {target}'s insertion point index is {index}")
print(f"Index of insertion point for element {target} is {index}")
# Array with duplicate elements
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
@@ -51,4 +51,4 @@ if __name__ == "__main__":
# Binary search for insertion point
for target in [2, 6, 20]:
index = binary_search_insertion(nums, target)
print(f"Element {target}'s insertion point index is {index}")
print(f"Index of insertion point for element {target} is {index}")
@@ -14,7 +14,7 @@ from modules import ListNode, list_to_linked_list
def hashing_search_array(hmap: dict[int, int], target: int) -> int:
"""Hash search (array)"""
# Hash table's key: target element, value: index
# If the hash table does not contain this key, return -1
# If this key does not exist in the hash table, return -1
return hmap.get(target, -1)
@@ -23,7 +23,7 @@ def hashing_search_linkedlist(
) -> ListNode | None:
"""Hash search (linked list)"""
# Hash table's key: target element, value: node object
# If the hash table does not contain this key, return None
# If this key does not exist in the hash table, return None
return hmap.get(target, None)
@@ -48,4 +48,4 @@ if __name__ == "__main__":
map1[head.val] = head # key: node value, value: node
head = head.next
node: ListNode = hashing_search_linkedlist(map1, target)
print("Target node value 3's corresponding node object is", node)
print("The corresponding node object for target node value 3 is", node)
@@ -13,21 +13,21 @@ from modules import ListNode, list_to_linked_list
def linear_search_array(nums: list[int], target: int) -> int:
"""Linear search (array)"""
# Traverse array
# Traverse the array
for i in range(len(nums)):
if nums[i] == target: # Found the target element, thus return its index
if nums[i] == target: # Found the target element, return its index
return i
return -1 # Did not find the target element, thus return -1
return -1 # Target element not found, return -1
def linear_search_linkedlist(head: ListNode, target: int) -> ListNode | None:
"""Linear search (linked list)"""
# Traverse the list
# Traverse the linked list
while head:
if head.val == target: # Found the target node, return it
return head
head = head.next
return None # Did not find the target node, thus return None
return None # Target node not found, return None
"""Driver Code"""
@@ -42,4 +42,4 @@ if __name__ == "__main__":
# Perform linear search in linked list
head: ListNode = list_to_linked_list(nums)
node: ListNode | None = linear_search_linkedlist(head, target)
print("Target node value 3's corresponding node object is", node)
print("The corresponding node object for target node value 3 is", node)
+8 -8
View File
@@ -6,8 +6,8 @@ Author: krahets (krahets@163.com)
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
"""Method one: Brute force enumeration"""
# Two-layer loop, time complexity is O(n^2)
"""Method 1: Brute force enumeration"""
# Two nested loops, time complexity is O(n^2)
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
@@ -16,10 +16,10 @@ def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
"""Method two: Auxiliary hash table"""
"""Method 2: Auxiliary hash table"""
# Auxiliary hash table, space complexity is O(n)
dic = {}
# Single-layer loop, time complexity is O(n)
# Single loop, time complexity is O(n)
for i in range(len(nums)):
if target - nums[i] in dic:
return [dic[target - nums[i]], i]
@@ -34,9 +34,9 @@ if __name__ == "__main__":
target = 13
# ====== Driver Code ======
# Method one
# Method 1
res: list[int] = two_sum_brute_force(nums, target)
print("Method one res =", res)
# Method two
print("Method 1 res =", res)
# Method 2
res: list[int] = two_sum_hash_table(nums, target)
print("Method two res =", res)
print("Method 2 res =", res)