mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 13:33:11 +08:00
feat(sorts): make reversort generic over comparable items (#15402)
* 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 * style(sorts): use PEP 695 type parameters per repo convention ruff UP047 requires the sanctioned [T: Comparable] signature form used by the reference insertion_sort.py. Keep the module-level TypeVar as in the reference. * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> --------- Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
co-authored by
Christian Clauss
parent
6a2ca6840c
commit
763b146ef2
+15
-3
@@ -17,10 +17,14 @@ For manual testing run:
|
||||
python reversort.py
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
from typing import Any, Protocol
|
||||
|
||||
|
||||
def reversort(collection: list[Any]) -> list[Any]:
|
||||
class Comparable(Protocol):
|
||||
def __lt__(self, other: Any, /) -> bool: ...
|
||||
|
||||
|
||||
def reversort[T: Comparable](collection: list[T]) -> list[T]:
|
||||
"""
|
||||
Sort a list using the Reversort algorithm.
|
||||
|
||||
@@ -78,6 +82,10 @@ def reversort(collection: list[Any]) -> list[Any]:
|
||||
>>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
|
||||
>>> reversort(collection_arg) == sorted(collection_arg)
|
||||
True
|
||||
>>> reversort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: '<' not supported between instances of 'str' and 'int'
|
||||
"""
|
||||
arr = collection[:] # Create a copy to avoid modifying the original
|
||||
n = len(arr)
|
||||
@@ -96,7 +104,7 @@ def reversort(collection: list[Any]) -> list[Any]:
|
||||
return arr
|
||||
|
||||
|
||||
def reversort_cost(collection: list[Any]) -> int:
|
||||
def reversort_cost[T: Comparable](collection: list[T]) -> int:
|
||||
"""
|
||||
Calculate the cost of sorting using Reversort.
|
||||
|
||||
@@ -119,6 +127,10 @@ def reversort_cost(collection: list[Any]) -> int:
|
||||
0
|
||||
>>> reversort_cost([])
|
||||
0
|
||||
>>> reversort_cost([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: '<' not supported between instances of 'str' and 'int'
|
||||
"""
|
||||
arr = collection[:] # Create a copy to avoid modifying the original
|
||||
n = len(arr)
|
||||
|
||||
@@ -41,6 +41,7 @@ from sorts.pancake_sort import pancake_sort
|
||||
from sorts.patience_sort import patience_sort
|
||||
from sorts.quick_sort import quick_sort
|
||||
from sorts.recursive_insertion_sort import rec_insertion_sort
|
||||
from sorts.reversort import reversort
|
||||
from sorts.selection_sort import selection_sort
|
||||
from sorts.shell_sort import shell_sort
|
||||
from sorts.shrink_shell_sort import shell_sort as shrink_shell_sort
|
||||
@@ -74,6 +75,7 @@ SORTS = (
|
||||
pancake_sort,
|
||||
patience_sort,
|
||||
quick_sort,
|
||||
reversort,
|
||||
selection_sort,
|
||||
shell_sort,
|
||||
shrink_shell_sort,
|
||||
@@ -141,6 +143,7 @@ def test_rec_insertion_sort(case) -> None:
|
||||
merge_sort,
|
||||
odd_even_sort,
|
||||
pancake_sort,
|
||||
reversort,
|
||||
selection_sort,
|
||||
shrink_shell_sort,
|
||||
strand_sort,
|
||||
|
||||
Reference in New Issue
Block a user