Skip to content

Commit eba0abd

Browse files
committed
Improve error message clarity in binary seach functions
1 parent 791deb4 commit eba0abd

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

searches/binary_search.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int:
198198
-1
199199
"""
200200
if any(a > b for a, b in pairwise(sorted_collection)):
201-
raise ValueError("sorted_collection must be sorted in ascending order")
201+
raise ValueError("Input list must be sorted in ascending order for binary search to work in ascending order")
202202
left = 0
203203
right = len(sorted_collection) - 1
204204

@@ -235,7 +235,7 @@ def binary_search_std_lib(sorted_collection: list[int], item: int) -> int:
235235
-1
236236
"""
237237
if list(sorted_collection) != sorted(sorted_collection):
238-
raise ValueError("sorted_collection must be sorted in ascending order")
238+
raise ValueError("Input list must be sorted in ascending order for binary search to work")
239239
index = bisect.bisect_left(sorted_collection, item)
240240
if index != len(sorted_collection) and sorted_collection[index] == item:
241241
return index
@@ -269,7 +269,7 @@ def binary_search_with_duplicates(sorted_collection: list[int], item: int) -> li
269269
[]
270270
"""
271271
if list(sorted_collection) != sorted(sorted_collection):
272-
raise ValueError("sorted_collection must be sorted in ascending order")
272+
raise ValueError("Input list must be sorted in ascending order for binary search to work")
273273

274274
def lower_bound(sorted_collection: list[int], item: int) -> int:
275275
"""
@@ -343,7 +343,7 @@ def binary_search_by_recursion(
343343
if right < 0:
344344
right = len(sorted_collection) - 1
345345
if list(sorted_collection) != sorted(sorted_collection):
346-
raise ValueError("sorted_collection must be sorted in ascending order")
346+
raise ValueError("Input list must be sorted in ascending order for binary search to work")
347347
if right < left:
348348
return -1
349349

@@ -382,7 +382,7 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
382382
-1
383383
"""
384384
if list(sorted_collection) != sorted(sorted_collection):
385-
raise ValueError("sorted_collection must be sorted in ascending order")
385+
raise ValueError("Input list must be sorted in ascending order for binary search to work")
386386
bound = 1
387387
while bound < len(sorted_collection) and sorted_collection[bound] < item:
388388
bound *= 2

0 commit comments

Comments
 (0)