From 5cf726a7936dff6063f253fc031253ef1da64f22 Mon Sep 17 00:00:00 2001 From: Yaswanth Naga Sai K <140506928+YASWANTH1976@users.noreply.github.com> Date: Sat, 3 Jan 2026 13:27:36 +0530 Subject: [PATCH 1/4] Improve sorted input validation in binary search --- searches/binary_search.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 5125dc6bdb9a..c74425f046fd 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,7 +198,10 @@ def binary_search(sorted_collection: list[int], item: int) -> int: >>> binary_search([0, 5, 7, 10, 15], 6) -1 """ - if list(sorted_collection) != sorted(sorted_collection): + if any( + sorted_collection[i] > sorted_collection[i + 1] + for i in range(len(sorted_collection) - 1) + ): raise ValueError("sorted_collection must be sorted in ascending order") left = 0 right = len(sorted_collection) - 1 From d1f6b9082d21a268c264514cff4de4c423b63dca Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 9 Mar 2026 08:46:57 +0300 Subject: [PATCH 2/4] Update binary_search.py --- searches/binary_search.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index c74425f046fd..93d8cf835b66 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,10 +198,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int: >>> binary_search([0, 5, 7, 10, 15], 6) -1 """ - if any( - sorted_collection[i] > sorted_collection[i + 1] - for i in range(len(sorted_collection) - 1) - ): + if all(a <= b for a, b in pairwise(sorted_collection)): raise ValueError("sorted_collection must be sorted in ascending order") left = 0 right = len(sorted_collection) - 1 From 368eabdb13cb1d2e643083110e0a8867461644ea Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 9 Mar 2026 08:47:46 +0300 Subject: [PATCH 3/4] Update binary_search.py --- searches/binary_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 93d8cf835b66..f9d1844bd5e7 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: >>> binary_search([0, 5, 7, 10, 15], 6) -1 """ - if all(a <= b for a, b in pairwise(sorted_collection)): + if any(a > b for a, b in pairwise(sorted_collection)): raise ValueError("sorted_collection must be sorted in ascending order") left = 0 right = len(sorted_collection) - 1 From 3746b08cec63aa6e87c1b5ad71a2dbe6d329cd5e Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 9 Mar 2026 08:48:26 +0300 Subject: [PATCH 4/4] Update binary_search.py --- searches/binary_search.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index f9d1844bd5e7..bec87b3c5aec 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -10,9 +10,8 @@ python3 binary_search.py """ -from __future__ import annotations - import bisect +from itertools import pairwise def bisect_left(