From 509c795c066381282b3eff3cd03c4f5645c45c7e Mon Sep 17 00:00:00 2001 From: TheRodzz <81969589+TheRodzz@users.noreply.github.com> Date: Wed, 5 Aug 2026 22:47:40 +0530 Subject: [PATCH] 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 --- machine_learning/sequential_minimum_optimization.py | 2 +- sorts/radix_sort.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py index e96f06d6f080..975a0172363d 100644 --- a/machine_learning/sequential_minimum_optimization.py +++ b/machine_learning/sequential_minimum_optimization.py @@ -451,7 +451,7 @@ def test_cancer_data(): print("Hello!\nStart test SVM using the SMO algorithm!") # 0: download dataset and load into pandas' dataframe if not os.path.exists(r"cancer_data.csv"): - request = urllib.request.Request( + request = urllib.request.Request( # noqa: S310, RUF100 CANCER_DATASET_URL, headers={"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}, ) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 1dbf5fbd1365..47c5dd8720e3 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -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: