Skip to content

Commit 509c795

Browse files
committed
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
1 parent f5988cc commit 509c795

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

machine_learning/sequential_minimum_optimization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def test_cancer_data():
451451
print("Hello!\nStart test SVM using the SMO algorithm!")
452452
# 0: download dataset and load into pandas' dataframe
453453
if not os.path.exists(r"cancer_data.csv"):
454-
request = urllib.request.Request(
454+
request = urllib.request.Request( # noqa: S310, RUF100
455455
CANCER_DATASET_URL,
456456
headers={"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"},
457457
)

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)