From eba0abdbf808862ddea6d605a55d9610b0df212b Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Sun, 3 May 2026 20:21:34 +0530 Subject: [PATCH 1/2] Improve error message clarity in binary seach functions --- searches/binary_search.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index bec87b3c5aec..15e5041f9a70 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,7 +198,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int: -1 """ if any(a > b for a, b in pairwise(sorted_collection)): - raise ValueError("sorted_collection must be sorted in ascending order") + raise ValueError("Input list must be sorted in ascending order for binary search to work in ascending order") left = 0 right = len(sorted_collection) - 1 @@ -235,7 +235,7 @@ def binary_search_std_lib(sorted_collection: list[int], item: int) -> int: -1 """ if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("sorted_collection must be sorted in ascending order") + raise ValueError("Input list must be sorted in ascending order for binary search to work") index = bisect.bisect_left(sorted_collection, item) if index != len(sorted_collection) and sorted_collection[index] == item: return index @@ -269,7 +269,7 @@ def binary_search_with_duplicates(sorted_collection: list[int], item: int) -> li [] """ if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("sorted_collection must be sorted in ascending order") + raise ValueError("Input list must be sorted in ascending order for binary search to work") def lower_bound(sorted_collection: list[int], item: int) -> int: """ @@ -343,7 +343,7 @@ def binary_search_by_recursion( if right < 0: right = len(sorted_collection) - 1 if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("sorted_collection must be sorted in ascending order") + raise ValueError("Input list must be sorted in ascending order for binary search to work") if right < left: return -1 @@ -382,7 +382,7 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: -1 """ if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("sorted_collection must be sorted in ascending order") + raise ValueError("Input list must be sorted in ascending order for binary search to work") bound = 1 while bound < len(sorted_collection) and sorted_collection[bound] < item: bound *= 2 From 572f8175f5eec3d152ecbc75a613864f5fe97588 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 3 May 2026 14:54:46 +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 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 15e5041f9a70..be5a95dd1080 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,7 +198,9 @@ def binary_search(sorted_collection: list[int], item: int) -> int: -1 """ if any(a > b for a, b in pairwise(sorted_collection)): - raise ValueError("Input list must be sorted in ascending order for binary search to work in ascending order") + raise ValueError( + "Input list must be sorted in ascending order for binary search to work in ascending order" + ) left = 0 right = len(sorted_collection) - 1 @@ -235,7 +237,9 @@ def binary_search_std_lib(sorted_collection: list[int], item: int) -> int: -1 """ if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("Input list must be sorted in ascending order for binary search to work") + raise ValueError( + "Input list must be sorted in ascending order for binary search to work" + ) index = bisect.bisect_left(sorted_collection, item) if index != len(sorted_collection) and sorted_collection[index] == item: return index @@ -269,7 +273,9 @@ def binary_search_with_duplicates(sorted_collection: list[int], item: int) -> li [] """ if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("Input list must be sorted in ascending order for binary search to work") + raise ValueError( + "Input list must be sorted in ascending order for binary search to work" + ) def lower_bound(sorted_collection: list[int], item: int) -> int: """ @@ -343,7 +349,9 @@ def binary_search_by_recursion( if right < 0: right = len(sorted_collection) - 1 if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("Input list must be sorted in ascending order for binary search to work") + raise ValueError( + "Input list must be sorted in ascending order for binary search to work" + ) if right < left: return -1 @@ -382,7 +390,9 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: -1 """ if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("Input list must be sorted in ascending order for binary search to work") + raise ValueError( + "Input list must be sorted in ascending order for binary search to work" + ) bound = 1 while bound < len(sorted_collection) and sorted_collection[bound] < item: bound *= 2