mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-07 21:24:18 +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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user