Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@
-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"

Check failure on line 202 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E501)

searches/binary_search.py:202:89: E501 Line too long (103 > 88)

Check failure on line 202 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E501)

searches/binary_search.py:202:89: E501 Line too long (103 > 88)

Check failure on line 202 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E501)

searches/binary_search.py:202:89: E501 Line too long (103 > 88)
)
left = 0
right = len(sorted_collection) - 1

Expand Down Expand Up @@ -235,7 +237,9 @@
-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
Expand Down Expand Up @@ -269,7 +273,9 @@
[]
"""
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:
"""
Expand Down Expand Up @@ -343,7 +349,9 @@
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

Expand Down Expand Up @@ -382,7 +390,9 @@
-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
Expand Down
Loading