From b80d47d171d422a5e165515a3b5bb517496ce782 Mon Sep 17 00:00:00 2001 From: Felipe Dev Date: Tue, 1 Sep 2026 11:36:12 -0300 Subject: [PATCH 1/3] fix(searches): return -1 for empty input in jump_search fix(sorts): validate cyclic_sort input to prevent infinite loops Fixes #15085 Fixes #14898 --- searches/jump_search.py | 5 +++++ sorts/cyclic_sort.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/searches/jump_search.py b/searches/jump_search.py index 437faf306bb2..8489bc8b8e0c 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -33,9 +33,14 @@ def jump_search[T: Comparable](arr: Sequence[T], item: T) -> int: 10 >>> jump_search(["aa", "bb", "cc", "dd", "ee", "ff"], "ee") 4 + >>> jump_search([], 5) + -1 """ arr_size = len(arr) + if arr_size == 0: + return -1 + block_size = int(math.sqrt(arr_size)) prev = 0 diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9e81291548d4..9b70d9e25865 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -27,8 +27,30 @@ def cyclic_sort(nums: list[int]) -> list[int]: [] >>> cyclic_sort([3, 5, 2, 1, 4]) [1, 2, 3, 4, 5] + >>> cyclic_sort([7, 3, 2, 3, 54, 5, 4]) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + ValueError: All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4] + >>> cyclic_sort([1, 2, 5]) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + ValueError: All numbers must be in range 1 to 3, got 5 """ + length = len(nums) + if length == 0: + return nums + + seen: set[int] = set() + for num in nums: + if num < 1 or num > length: + raise ValueError( + f"All numbers must be in range 1 to {length}, got {num}" + ) + if num in seen: + raise ValueError(f"All numbers must be unique, got {nums}") + seen.add(num) + # Perform cyclic sort index = 0 while index < len(nums): From 2fb19fbaf00690555cf95e5de867c9522212a73d Mon Sep 17 00:00:00 2001 From: Felipe Dev Date: Tue, 1 Sep 2026 11:44:40 -0300 Subject: [PATCH 2/3] fix(sorts): assign ValueError messages before raising (EM102) --- sorts/cyclic_sort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9b70d9e25865..de484c5105f3 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -44,11 +44,11 @@ def cyclic_sort(nums: list[int]) -> list[int]: seen: set[int] = set() for num in nums: if num < 1 or num > length: - raise ValueError( - f"All numbers must be in range 1 to {length}, got {num}" - ) + msg = f"All numbers must be in range 1 to {length}, got {num}" + raise ValueError(msg) if num in seen: - raise ValueError(f"All numbers must be unique, got {nums}") + msg = f"All numbers must be unique, got {nums}" + raise ValueError(msg) seen.add(num) # Perform cyclic sort From 3164561d682d9c3d6700b410e097204ea5d9d2a6 Mon Sep 17 00:00:00 2001 From: Felipe Dev Date: Tue, 1 Sep 2026 11:47:08 -0300 Subject: [PATCH 3/3] chore: sync PR head with ruff EM102 fix