mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 13:33:11 +08:00
* 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>
This commit is contained in:
co-authored by
Christian Clauss
parent
8870905dc2
commit
5a6cc4869a
@@ -21,7 +21,17 @@ def radix_sort(list_of_ints: list[int]) -> list[int]:
|
||||
True
|
||||
>>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000])
|
||||
True
|
||||
>>> radix_sort([-1, 2, 3])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: All elements in list_of_ints must be non-negative integers
|
||||
"""
|
||||
if not list_of_ints:
|
||||
return []
|
||||
|
||||
if any(i < 0 for i in list_of_ints):
|
||||
raise ValueError("All elements in list_of_ints must be non-negative integers")
|
||||
|
||||
placement = 1
|
||||
max_digit = max(list_of_ints)
|
||||
while placement <= max_digit:
|
||||
|
||||
Reference in New Issue
Block a user