Skip to content

Commit 7af5aba

Browse files
Improve sorted input validation in binary search (#14074)
* Improve sorted input validation in binary search * Update binary_search.py * Update binary_search.py * Update binary_search.py --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
1 parent 589d129 commit 7af5aba

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

searches/binary_search.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
python3 binary_search.py
1111
"""
1212

13-
from __future__ import annotations
14-
1513
import bisect
14+
from itertools import pairwise
1615

1716

1817
def bisect_left(
@@ -198,7 +197,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int:
198197
>>> binary_search([0, 5, 7, 10, 15], 6)
199198
-1
200199
"""
201-
if list(sorted_collection) != sorted(sorted_collection):
200+
if any(a > b for a, b in pairwise(sorted_collection)):
202201
raise ValueError("sorted_collection must be sorted in ascending order")
203202
left = 0
204203
right = len(sorted_collection) - 1

0 commit comments

Comments
 (0)