From f126613abd6a1781c1a1467ce231bc51b38aebc2 Mon Sep 17 00:00:00 2001 From: Saadanjum0 <160885369+Saadanjum0@users.noreply.github.com> Date: Fri, 2 Oct 2026 13:58:17 +0500 Subject: [PATCH] Fix quick_select crashing on non-positive rank in median_of_medians.py quick_select only rejected target > len(arr), so a rank of 0 or negative fell through to median_of_medians() on a progressively smaller list until it indexed an empty list, raising IndexError instead of returning -1 like other invalid input. Added doctests covering target = 0 and target = -1. --- searches/median_of_medians.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/searches/median_of_medians.py b/searches/median_of_medians.py index a8011a34af76..dd46613c2bff 100644 --- a/searches/median_of_medians.py +++ b/searches/median_of_medians.py @@ -74,10 +74,14 @@ def quick_select(arr: list, target: int) -> int: 3 >>> quick_select([3, 5, 7, 10, 2, 12], 3) 5 + >>> quick_select([2, 4, 5, 7, 899, 54, 32], 0) + -1 + >>> quick_select([2, 4, 5, 7, 899, 54, 32], -1) + -1 """ # Invalid Input - if target > len(arr): + if target <= 0 or target > len(arr): return -1 # x is the estimated pivot by median of medians algorithm