From 1ff9104def11424261607eee6ef4c4f1d5e4ba90 Mon Sep 17 00:00:00 2001 From: Yaswanth Naga Sai K <140506928+YASWANTH1976@users.noreply.github.com> Date: Sat, 3 Jan 2026 12:35:31 +0530 Subject: [PATCH] Improve sorted input validation in binary search --- searches/binary_search.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 5125dc6bdb9a..80fc0880bf3b 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,8 +198,11 @@ 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): - raise ValueError("sorted_collection must be sorted in ascending order") + 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