Skip to content

Commit a3d5b3b

Browse files
committed
feat(sorts): make reversort generic over comparable items
Part of #15234 - Replace list[Any] with a Comparable-bounded TypeVar so reversort and reversort_cost sort any mutually comparable items, not just ints - Add TypeError doctests confirming mixed non-comparable input raises - Register reversort in the shared test battery covering str/float/ dataclass/NamedTuple cases and the non-comparable rejection path
1 parent 42c4c73 commit a3d5b3b

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎sorts/reversort.py‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
python reversort.py
1818
"""
1919

20-
from typing import Any
20+
from typing import Any, Protocol, TypeVar
2121

2222

23-
def reversort(collection: list[Any]) -> list[Any]:
23+
class Comparable(Protocol):
24+
def __lt__(self, other: Any, /) -> bool: ...
25+
26+
27+
T = TypeVar("T", bound=Comparable)
28+
29+
30+
def reversort(collection: list[T]) -> list[T]:
2431
"""
2532
Sort a list using the Reversort algorithm.
2633
@@ -78,6 +85,10 @@ def reversort(collection: list[Any]) -> list[Any]:
7885
>>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
7986
>>> reversort(collection_arg) == sorted(collection_arg)
8087
True
88+
>>> reversort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
89+
Traceback (most recent call last):
90+
...
91+
TypeError: '<' not supported between instances of 'str' and 'int'
8192
"""
8293
arr = collection[:] # Create a copy to avoid modifying the original
8394
n = len(arr)
@@ -96,7 +107,7 @@ def reversort(collection: list[Any]) -> list[Any]:
96107
return arr
97108

98109

99-
def reversort_cost(collection: list[Any]) -> int:
110+
def reversort_cost(collection: list[T]) -> int:
100111
"""
101112
Calculate the cost of sorting using Reversort.
102113
@@ -119,6 +130,10 @@ def reversort_cost(collection: list[Any]) -> int:
119130
0
120131
>>> reversort_cost([])
121132
0
133+
>>> reversort_cost([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
134+
Traceback (most recent call last):
135+
...
136+
TypeError: '<' not supported between instances of 'str' and 'int'
122137
"""
123138
arr = collection[:] # Create a copy to avoid modifying the original
124139
n = len(arr)

‎tests/test_sorts.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from sorts.patience_sort import patience_sort
4141
from sorts.quick_sort import quick_sort
4242
from sorts.recursive_insertion_sort import rec_insertion_sort
43+
from sorts.reversort import reversort
4344
from sorts.selection_sort import selection_sort
4445
from sorts.shell_sort import shell_sort
4546
from sorts.shrink_shell_sort import shell_sort as shrink_shell_sort
@@ -73,6 +74,7 @@ def test_heap_sort() -> None:
7374
pancake_sort,
7475
patience_sort,
7576
quick_sort,
77+
reversort,
7678
selection_sort,
7779
shell_sort,
7880
shrink_shell_sort,
@@ -140,6 +142,7 @@ def test_rec_insertion_sort(case) -> None:
140142
merge_sort,
141143
odd_even_sort,
142144
pancake_sort,
145+
reversort,
143146
selection_sort,
144147
shrink_shell_sort,
145148
strand_sort,

0 commit comments

Comments
 (0)