Skip to content

Commit 720680c

Browse files
committed
Add input validation to radix_sort iss#14950
1 parent c0db072 commit 720680c

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

sorts/radix_sort.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def radix_sort(list_of_ints: list[int]) -> list[int]:
1414
Examples:
1515
>>> radix_sort([0, 5, 3, 2, 2])
1616
[0, 2, 2, 3, 5]
17+
>>> radix_sort([2, 4, -2, 1, 3])
18+
Traceback (most recent call last):
19+
...
20+
ValueError: Negative numbers are not supported.
1721
1822
>>> radix_sort(list(range(15))) == sorted(range(15))
1923
True
@@ -29,6 +33,9 @@ def radix_sort(list_of_ints: list[int]) -> list[int]:
2933
buckets: list[list] = [[] for _ in range(RADIX)]
3034
# split list_of_ints between the buckets
3135
for i in list_of_ints:
36+
# check for negative numbers
37+
if i < 0:
38+
raise ValueError("Negative numbers are not supported.")
3239
tmp = int((i / placement) % RADIX)
3340
buckets[tmp].append(i)
3441
# put each buckets' contents into list_of_ints

0 commit comments

Comments
 (0)