diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 1dbf5fbd1365..7571826908bb 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -11,17 +11,27 @@ def radix_sort(list_of_ints: list[int]) -> list[int]: """ - Examples: + Sort a list of non-negative integers using radix sort. + + This implementation works only with non-negative values because it + iterates over the decimal digits of each number. + >>> radix_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - >>> radix_sort(list(range(15))) == sorted(range(15)) True - >>> radix_sort(list(range(14,-1,-1))) == sorted(range(15)) + >>> radix_sort(list(range(14, -1, -1))) == sorted(range(15)) True - >>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000]) + >>> radix_sort([1, 100, 10, 1000]) == sorted([1, 100, 10, 1000]) True + >>> radix_sort([3, 1, -1, 2]) + Traceback (most recent call last): + ... + ValueError: All numbers in list_of_ints must be non-negative """ + if any(item < 0 for item in list_of_ints): + raise ValueError("All numbers in list_of_ints must be non-negative") + placement = 1 max_digit = max(list_of_ints) while placement <= max_digit: