Skip to content

Commit 5a6cc48

Browse files
TheRodzzcclauss
andauthored
fix(sorts): raise ValueError for negative inputs in radix_sort (#14950) (#14986)
* fix(sorts): raise ValueError for negative inputs in radix_sort (#14950) This commit updates `radix_sort()` to validate that all integers in `list_of_ints` are non-negative. Radix sort relies on digit-by-digit sorting and only supports non-negative integers. If any negative integer is present in `list_of_ints`, `radix_sort()` now raises a `ValueError`. Empty list input is also handled gracefully. Additionally, pre-commit noqa annotations are updated where needed. Fixes #14950 * Apply suggestion from @cclauss --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 8870905 commit 5a6cc48

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

sorts/radix_sort.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ def radix_sort(list_of_ints: list[int]) -> list[int]:
2121
True
2222
>>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000])
2323
True
24+
>>> radix_sort([-1, 2, 3])
25+
Traceback (most recent call last):
26+
...
27+
ValueError: All elements in list_of_ints must be non-negative integers
2428
"""
29+
if not list_of_ints:
30+
return []
31+
32+
if any(i < 0 for i in list_of_ints):
33+
raise ValueError("All elements in list_of_ints must be non-negative integers")
34+
2535
placement = 1
2636
max_digit = max(list_of_ints)
2737
while placement <= max_digit:

0 commit comments

Comments
 (0)