Skip to content

Commit cbe06a4

Browse files
fix(sorts): raise ValueError on negative inputs in radix_sort (#14950)
1 parent f5988cc commit cbe06a4

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

sorts/radix_sort.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@
1111

1212
def radix_sort(list_of_ints: list[int]) -> list[int]:
1313
"""
14-
Examples:
14+
Sort a list of non-negative integers using radix sort.
15+
16+
This implementation works only with non-negative values because it
17+
iterates over the decimal digits of each number.
18+
1519
>>> radix_sort([0, 5, 3, 2, 2])
1620
[0, 2, 2, 3, 5]
17-
1821
>>> radix_sort(list(range(15))) == sorted(range(15))
1922
True
20-
>>> radix_sort(list(range(14,-1,-1))) == sorted(range(15))
23+
>>> radix_sort(list(range(14, -1, -1))) == sorted(range(15))
2124
True
22-
>>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000])
25+
>>> radix_sort([1, 100, 10, 1000]) == sorted([1, 100, 10, 1000])
2326
True
27+
>>> radix_sort([3, 1, -1, 2])
28+
Traceback (most recent call last):
29+
...
30+
ValueError: All numbers in list_of_ints must be non-negative
2431
"""
32+
if any(item < 0 for item in list_of_ints):
33+
raise ValueError("All numbers in list_of_ints must be non-negative")
34+
2535
placement = 1
2636
max_digit = max(list_of_ints)
2737
while placement <= max_digit:

0 commit comments

Comments
 (0)