From 720680cfb50243f04c7f6018190ca5f0af0fa264 Mon Sep 17 00:00:00 2001 From: Michael Green Date: Mon, 20 Jul 2026 18:07:04 -0700 Subject: [PATCH] Add input validation to radix_sort iss#14950 --- sorts/radix_sort.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 1dbf5fbd1365..d69f9163428f 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -14,6 +14,10 @@ def radix_sort(list_of_ints: list[int]) -> list[int]: Examples: >>> radix_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] + >>> radix_sort([2, 4, -2, 1, 3]) + Traceback (most recent call last): + ... + ValueError: Negative numbers are not supported. >>> radix_sort(list(range(15))) == sorted(range(15)) True @@ -29,6 +33,9 @@ def radix_sort(list_of_ints: list[int]) -> list[int]: buckets: list[list] = [[] for _ in range(RADIX)] # split list_of_ints between the buckets for i in list_of_ints: + # check for negative numbers + if i < 0: + raise ValueError("Negative numbers are not supported.") tmp = int((i / placement) % RADIX) buckets[tmp].append(i) # put each buckets' contents into list_of_ints