From 21b512a5845335643e2049b95cb298e67f2cf9d8 Mon Sep 17 00:00:00 2001 From: Pranav Date: Sun, 12 Jul 2026 23:38:01 +0530 Subject: [PATCH 1/3] Add input validation to cyclic_sort --- sorts/cyclic_sort.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9e81291548d4..5dea6f367408 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -27,11 +27,26 @@ def cyclic_sort(nums: list[int]) -> list[int]: [] >>> cyclic_sort([3, 5, 2, 1, 4]) [1, 2, 3, 4, 5] - """ + >>> cyclic_sort([1, 2, 5]) + Traceback (most recent call last): + ... + ValueError: All numbers must be in range 1 and 3, got 5. + >>> cyclic_sort([7, 3, 2, 3, 54, 5, 4]) + Traceback (most recent call last): + ... + ValueError: All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4]. + """ + # Reject invalid input to prevent infinite loops and invalid indexing. + n = len(nums) + if len(set(nums)) != n: + raise ValueError(f"All numbers must be unique, got {nums}.") + for num in nums: + if not 1 <= num <= n: + raise ValueError(f"All numbers must be in range 1 and {n}, got {num}.") # Perform cyclic sort index = 0 - while index < len(nums): + while index < n: # Calculate the correct index for the current element correct_index = nums[index] - 1 # If the current element is not at its correct position, From eb5e9dc2e5850aaad9c9f94bb77598e5a8908668 Mon Sep 17 00:00:00 2001 From: Pranav Date: Sun, 12 Jul 2026 23:47:17 +0530 Subject: [PATCH 2/3] Address Ruff linting feedback --- sorts/cyclic_sort.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 5dea6f367408..5b544afbf53a 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -30,7 +30,7 @@ def cyclic_sort(nums: list[int]) -> list[int]: >>> cyclic_sort([1, 2, 5]) Traceback (most recent call last): ... - ValueError: All numbers must be in range 1 and 3, got 5. + ValueError: All numbers must be in range 1 to 3, got 5. >>> cyclic_sort([7, 3, 2, 3, 54, 5, 4]) Traceback (most recent call last): @@ -40,10 +40,12 @@ def cyclic_sort(nums: list[int]) -> list[int]: # Reject invalid input to prevent infinite loops and invalid indexing. n = len(nums) if len(set(nums)) != n: - raise ValueError(f"All numbers must be unique, got {nums}.") + err=f"All numbers must be unique, got {nums}." + raise ValueError(err) for num in nums: if not 1 <= num <= n: - raise ValueError(f"All numbers must be in range 1 and {n}, got {num}.") + err=f"All numbers must be in range 1 to {n}, got {num}." + raise ValueError(err) # Perform cyclic sort index = 0 while index < n: From d1229d77d945311f789724ed9987607778a3616a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Jul 2026 18:17:36 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/cyclic_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 5b544afbf53a..ad965eab0e82 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -40,11 +40,11 @@ def cyclic_sort(nums: list[int]) -> list[int]: # Reject invalid input to prevent infinite loops and invalid indexing. n = len(nums) if len(set(nums)) != n: - err=f"All numbers must be unique, got {nums}." + err = f"All numbers must be unique, got {nums}." raise ValueError(err) for num in nums: if not 1 <= num <= n: - err=f"All numbers must be in range 1 to {n}, got {num}." + err = f"All numbers must be in range 1 to {n}, got {num}." raise ValueError(err) # Perform cyclic sort index = 0