From 6379ae3fc631d786407c6c340924002af403a9b9 Mon Sep 17 00:00:00 2001 From: Akshat Gairola Date: Thu, 6 Aug 2026 11:09:30 +0530 Subject: [PATCH 1/5] Add input validation to cyclic_sort --- sorts/cyclic_sort.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9e81291548d4..3dfdcbec96dd 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -28,6 +28,19 @@ def cyclic_sort(nums: list[int]) -> list[int]: >>> cyclic_sort([3, 5, 2, 1, 4]) [1, 2, 3, 4, 5] """ + # Input validation + seen = set() + + for i in range(len(nums)): + if nums[i] in seen: + raise ValueError("All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4]") + + elif nums[i] < 1 or nums[i] > len(nums): + raise ValueError("All numbers must be in range 1 to 3, got 5") + + else: + seen.add(nums[i]) + # Perform cyclic sort index = 0 @@ -49,6 +62,7 @@ def cyclic_sort(nums: list[int]) -> list[int]: if __name__ == "__main__": import doctest + doctest.testmod() user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From 163b615d196bd3797fa9cb32918371a2fc939406 Mon Sep 17 00:00:00 2001 From: Akshat Gairola Date: Thu, 6 Aug 2026 11:19:55 +0530 Subject: [PATCH 2/5] Add input validation to cyclic_sort --- sorts/cyclic_sort.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 3dfdcbec96dd..3642991c50a7 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -27,19 +27,31 @@ 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 to 3, got 5 + + >>> cyclic_sort([1, 2, 2]) + Traceback (most recent call last): + ... + ValueError: All numbers must be unique, got [1, 2, 2] """ - # Input validation + # Input validation seen = set() + n = len(nums) - for i in range(len(nums)): - if nums[i] in seen: - raise ValueError("All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4]") + for num in nums: + if num in seen: + raise ValueError(f"All numbers must be unique, got {nums}") - elif nums[i] < 1 or nums[i] > len(nums): - raise ValueError("All numbers must be in range 1 to 3, got 5") + if num < 1 or num > n: + raise ValueError( + f"All numbers must be in range 1 to {n}, got {num}" + ) - else: - seen.add(nums[i]) + seen.add(num) # Perform cyclic sort From 2a080f271f99ff0a98348ec01c1a498065508206 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Aug 2026 05:59:25 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/cyclic_sort.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 3642991c50a7..6d964a1e6a74 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -28,7 +28,7 @@ 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 to 3, got 5 @@ -47,13 +47,10 @@ def cyclic_sort(nums: list[int]) -> list[int]: raise ValueError(f"All numbers must be unique, got {nums}") if num < 1 or num > n: - raise ValueError( - f"All numbers must be in range 1 to {n}, got {num}" - ) + raise ValueError(f"All numbers must be in range 1 to {n}, got {num}") seen.add(num) - # Perform cyclic sort index = 0 while index < len(nums): @@ -74,7 +71,6 @@ def cyclic_sort(nums: list[int]) -> list[int]: if __name__ == "__main__": import doctest - doctest.testmod() user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From 9d48bf882dbc98108d934e1ff180ac8fda3787b6 Mon Sep 17 00:00:00 2001 From: Akshat Gairola Date: Thu, 6 Aug 2026 11:51:01 +0530 Subject: [PATCH 4/5] f string literal bug resolved --- sorts/cyclic_sort.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 6d964a1e6a74..9bf82f94f948 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -5,6 +5,7 @@ python -m doctest -v cyclic_sort.py or python3 -m doctest -v cyclic_sort.py + For manual testing run: python cyclic_sort.py or @@ -27,42 +28,34 @@ 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 to 3, got 5 - - >>> cyclic_sort([1, 2, 2]) - Traceback (most recent call last): - ... - ValueError: All numbers must be unique, got [1, 2, 2] """ + # Input validation seen = set() n = len(nums) for num in nums: if num in seen: - raise ValueError(f"All numbers must be unique, got {nums}") + message = f"All numbers must be unique, got {nums}" + raise ValueError(message) if num < 1 or num > n: - raise ValueError(f"All numbers must be in range 1 to {n}, got {num}") + message = f"All numbers must be in range 1 to {n}, got {num}" + raise ValueError(message) seen.add(num) # Perform cyclic sort index = 0 while index < len(nums): - # Calculate the correct index for the current element correct_index = nums[index] - 1 - # If the current element is not at its correct position, - # swap it with the element at its correct index + if index != correct_index: - nums[index], nums[correct_index] = nums[correct_index], nums[index] + nums[index], nums[correct_index] = ( + nums[correct_index], + nums[index], + ) else: - # If the current element is already in its correct position, - # move to the next element index += 1 return nums @@ -72,6 +65,7 @@ def cyclic_sort(nums: list[int]) -> list[int]: import doctest doctest.testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(*cyclic_sort(unsorted), sep=",") + print(*cyclic_sort(unsorted), sep=",") \ No newline at end of file From 09f3e803b9b9c1e2dab2eb8a8526e212f0f5c88a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Aug 2026 06:37:29 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/cyclic_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9bf82f94f948..554cafb14917 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -68,4 +68,4 @@ def cyclic_sort(nums: list[int]) -> list[int]: user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(*cyclic_sort(unsorted), sep=",") \ No newline at end of file + print(*cyclic_sort(unsorted), sep=",")