Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"master"
]
],
"python-envs.defaultEnvManager": "ms-python.python:system"
}
4 changes: 4 additions & 0 deletions sorts/radix_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def radix_sort(list_of_ints: list[int]) -> list[int]:
>>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000])
True
"""

if any(i < 0 for i in list_of_ints):
raise ValueError("radix_sort only supports non-negative integers")

placement = 1
max_digit = max(list_of_ints)
while placement <= max_digit:
Expand Down
55 changes: 55 additions & 0 deletions sorts/test_radix_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest

from sorts.radix_sort import radix_sort


def test_radix_sort_basic():
assert radix_sort([0, 5, 3, 2, 2]) == [0, 2, 2, 3, 5]


def test_radix_sort_already_sorted():
values = [1, 2, 3, 4, 5]
assert radix_sort(values) == [1, 2, 3, 4, 5]


def test_radix_sort_reverse_sorted():
values = [5, 4, 3, 2, 1]
assert radix_sort(values) == [1, 2, 3, 4, 5]


def test_radix_sort_duplicates():
values = [4, 2, 4, 1, 2, 4]
assert radix_sort(values) == [1, 2, 2, 4, 4, 4]


def test_radix_sort_zero():
assert radix_sort([0]) == [0]


def test_radix_sort_multiple_zeros():
assert radix_sort([0, 5, 0, 2, 3]) == [0, 0, 2, 3, 5]


def test_radix_sort_different_digit_lengths():
values = [1, 1000, 10, 100, 10000]
assert radix_sort(values) == [1, 10, 100, 1000, 10000]


def test_radix_sort_large_numbers():
values = [999999, 123456, 1000000, 42]
assert radix_sort(values) == [42, 123456, 999999, 1000000]


def test_radix_sort_negative_number():
with pytest.raises(ValueError):
radix_sort([-1, 5, 3])


def test_radix_sort_multiple_negative_numbers():
with pytest.raises(ValueError):
radix_sort([-10, -5, 0, 5])


def test_radix_sort_matches_sorted():
values = [170, 45, 75, 90, 802, 24, 2, 66]
assert radix_sort(values) == sorted(values)