From 739d170df89e23a22147ffca67928e9c8e0c3d95 Mon Sep 17 00:00:00 2001 From: PieBerlin Date: Tue, 11 Nov 2025 16:18:23 +0300 Subject: [PATCH 1/2] simplifiying the binary search --- searches/binary_search.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/searches/binary_search.py b/searches/binary_search.py index 2e66b672d5b4..d82ad510d59c 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -321,6 +321,28 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: return -1 return last_result +#binary search uses divide and conquer +#We will leverage the fact that our list is sorted +def binary_search2(l,target, low=None,high=None): + if low is None: + low=0 + if high is None: + high=len(l)-1 + if high < low: + return -1 + + midpoint=(low+high) // 2 + if l[midpoint] == target: + return midpoint + elif target int: print(f"{target} was not found in {collection}.") else: print(f"{target} was found at position {result} of {collection}.") + + #Testing the binary search 2 + mylist=[1,2,3,4,5,6,7,8,9,10] + print(binary_search(mylist,10) ) From 7e9d4487a7db73b9283f6921c5ee75363106c040 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Nov 2025 13:25:30 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/binary_search.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index d82ad510d59c..70dcb723b70d 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -321,27 +321,24 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: return -1 return last_result -#binary search uses divide and conquer -#We will leverage the fact that our list is sorted -def binary_search2(l,target, low=None,high=None): + +# binary search uses divide and conquer +# We will leverage the fact that our list is sorted +def binary_search2(l, target, low=None, high=None): if low is None: - low=0 + low = 0 if high is None: - high=len(l)-1 + high = len(l) - 1 if high < low: return -1 - - midpoint=(low+high) // 2 + + midpoint = (low + high) // 2 if l[midpoint] == target: return midpoint - elif target