Skip to content

Commit edb660a

Browse files
committed
fix: add input validation to cyclic_sort (#14898)
### Describe your change: Added input validation to cyclic_sort function to prevent infinite loops and silent errors when the input list contains duplicates or numbers outside the valid range. * [ ] Add an algorithm? * [x] Fix a bug or typo in an existing algorithm? * [ ] Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request. * [ ] Documentation change? ### Checklist: * [x] I have read CONTRIBUTING.md. * [x] This pull request is all my own work -- I have not plagiarized. * [x] I know that pull requests will not be merged if they fail the automated tests. * [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms. * [x] All new Python files are placed inside an existing directory. * [x] All filenames are in all lowercase characters with no spaces or dashes. * [x] All functions and variable names follow Python naming conventions. * [x] All function parameters and return values are annotated with Python type hints. * [x] All functions have doctests that pass the automated testing. * [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation. * [x] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: Fixes #14898
1 parent f5988cc commit edb660a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

sorts/cyclic_sort.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def cyclic_sort(nums: list[int]) -> list[int]:
1919
2020
:param nums: List of n integers from 1 to n to be sorted.
2121
:return: The same list sorted in ascending order.
22+
:raises ValueError: If input contains duplicate numbers.
23+
:raises ValueError: If input contains numbers outside range 1 to n.
2224
2325
Time complexity: O(n), where n is the number of integers in the list.
2426
@@ -27,7 +29,37 @@ def cyclic_sort(nums: list[int]) -> list[int]:
2729
[]
2830
>>> cyclic_sort([3, 5, 2, 1, 4])
2931
[1, 2, 3, 4, 5]
32+
>>> cyclic_sort([1])
33+
[1]
34+
>>> cyclic_sort([2, 1])
35+
[1, 2]
36+
37+
>>> cyclic_sort([7, 3, 2, 3, 54, 5, 4])
38+
Traceback (most recent call last):
39+
...
40+
ValueError: All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4]
41+
42+
>>> cyclic_sort([1, 2, 5])
43+
Traceback (most recent call last):
44+
...
45+
ValueError: All numbers must be in range 1 to 3, got 5
3046
"""
47+
n = len(nums)
48+
49+
# Empty list is already sorted
50+
if n == 0:
51+
return nums
52+
53+
# Check for duplicates
54+
if len(set(nums)) != n:
55+
raise ValueError(f"All numbers must be unique, got {nums}")
56+
57+
# Check if all numbers are in range 1 to n
58+
for num in nums:
59+
if num < 1 or num > n:
60+
raise ValueError(
61+
f"All numbers must be in range 1 to {n}, got {num}"
62+
)
3163

3264
# Perform cyclic sort
3365
index = 0

0 commit comments

Comments
 (0)