Skip to content

Commit bf00f19

Browse files
author
deepshekhardas
committed
fix(cyclic_sort): validate input to prevent infinite loop
Fixes #14898
1 parent f5988cc commit bf00f19

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

sorts/cyclic_sort.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def cyclic_sort(nums: list[int]) -> list[int]:
2929
[1, 2, 3, 4, 5]
3030
"""
3131

32+
if len(nums) != len(set(nums)):
33+
raise ValueError(f"All numbers must be unique, got {nums}")
34+
if nums and (min(nums) < 1 or max(nums) > len(nums)):
35+
raise ValueError(f"All numbers must be in range 1 to {len(nums)}, got {nums}")
36+
3237
# Perform cyclic sort
3338
index = 0
3439
while index < len(nums):

0 commit comments

Comments
 (0)