From eba0abdbf808862ddea6d605a55d9610b0df212b Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Sun, 3 May 2026 20:21:34 +0530 Subject: [PATCH 1/6] 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/6] [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 From 52fb4588979805643966449e44bbc21fc7cdb8b6 Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Sun, 3 May 2026 20:44:46 +0530 Subject: [PATCH 3/6] Fix duplicate raise and formatting issue --- searches/binary_search.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index be5a95dd1080..2cafee7bfc91 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -391,8 +391,10 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError( - "Input list must be sorted in ascending order for binary search to work" + "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 6891fee5df11979ac753fd296cbf4090b056ed5f 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 15:15:58 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/binary_search.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 2cafee7bfc91..40ba9bc9ab7e 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -391,10 +391,9 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError( - "Input list must be sorted in ascending order" - "for binary search to work" + "Input list must be sorted in ascending orderfor binary search to work" ) - + bound = 1 while bound < len(sorted_collection) and sorted_collection[bound] < item: bound *= 2 From 036698dc1233dc922624efcbfa89cd66896ce914 Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Sun, 3 May 2026 21:01:26 +0530 Subject: [PATCH 5/6] Fix ruff errors(line length and spacing) --- searches/binary_search.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 40ba9bc9ab7e..d861e73de8b4 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -199,7 +199,8 @@ def binary_search(sorted_collection: list[int], item: int) -> int: """ 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" + "Input list must be sorted in ascending order " + "for binary search to work" ) left = 0 right = len(sorted_collection) - 1 @@ -238,7 +239,8 @@ def binary_search_std_lib(sorted_collection: list[int], item: int) -> int: """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError( - "Input list must be sorted in ascending order for binary search to work" + "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: @@ -274,7 +276,8 @@ 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" + "Input list must be sorted in ascending order " + "for binary search to work" ) def lower_bound(sorted_collection: list[int], item: int) -> int: @@ -391,7 +394,8 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError( - "Input list must be sorted in ascending orderfor binary search to work" + "Input list must be sorted in ascending order " + "for binary search to work" ) bound = 1 From 993bb7a0139b386ee4876f50631a2487d6c43293 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 15:32:36 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/binary_search.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index d861e73de8b4..2ca783bc6fdc 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -199,8 +199,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int: """ 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" + "Input list must be sorted in ascending order for binary search to work" ) left = 0 right = len(sorted_collection) - 1 @@ -239,8 +238,7 @@ def binary_search_std_lib(sorted_collection: list[int], item: int) -> int: """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError( - "Input list must be sorted in ascending order " - "for binary search to work" + "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: @@ -276,8 +274,7 @@ 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" + "Input list must be sorted in ascending order for binary search to work" ) def lower_bound(sorted_collection: list[int], item: int) -> int: @@ -394,8 +391,7 @@ def exponential_search(sorted_collection: list[int], item: int) -> int: """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError( - "Input list must be sorted in ascending order " - "for binary search to work" + "Input list must be sorted in ascending order for binary search to work" ) bound = 1