Skip to content

Commit e49caf5

Browse files
AuroraAeonAuroraEoncclauss
authored
types(reverse_selection): constrain items to Comparable (#15410)
* types(reverse_selection): constrain items to Comparable Bind reverse_selection_sort's element type to a Comparable Protocol so the signature says "a list of items that can be compared with each other" instead of a bare list, and keep the element type in the return. reverse_subarray only swaps elements and never compares them, so its TypeVar stays unbounded. Also adds doctests for a comparable non-int type (strings, floats) and for the failure mode: mixing non-comparable items must raise TypeError rather than silently mis-sort. The test battery picks the sort up for the shared cases and for the rejection check. * updating DIRECTORY.md --------- Co-authored-by: AuroraAeon <auroraeon@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 1d64d4c commit e49caf5

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
* [Permutations](data_structures/arrays/permutations.py)
277277
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
278278
* [Product Sum](data_structures/arrays/product_sum.py)
279+
* [Reverse Array](data_structures/arrays/reverse_array.py)
279280
* [Rotate Array](data_structures/arrays/rotate_array.py)
280281
* [Set Matrix Zeroes](data_structures/arrays/set_matrix_zeroes.py)
281282
* [Sparse Table](data_structures/arrays/sparse_table.py)

‎sorts/reverse_selection.py‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
This algorithm progressively sorts the array by reversing subarrays
55
66
For doctests run following command:
7-
python3 -m doctest -v reverse_selection_sort.py
7+
python3 -m doctest -v reverse_selection.py
88
99
For manual testing run:
10-
python3 reverse_selection_sort.py
10+
python3 reverse_selection.py
1111
"""
1212

13+
from typing import Any, Protocol
1314

14-
def reverse_subarray(arr: list, start: int, end: int) -> None:
15+
16+
class Comparable(Protocol):
17+
def __lt__(self, other: Any, /) -> bool: ...
18+
19+
20+
def reverse_subarray[T](arr: list[T], start: int, end: int) -> None:
1521
"""
1622
Reverse a subarray in-place.
1723
@@ -41,7 +47,7 @@ def reverse_subarray(arr: list, start: int, end: int) -> None:
4147
end -= 1
4248

4349

44-
def reverse_selection_sort(collection: list) -> list:
50+
def reverse_selection_sort[T: Comparable](collection: list[T]) -> list[T]:
4551
"""
4652
A pure implementation of reverse selection sort algorithm in Python
4753
@@ -64,6 +70,17 @@ def reverse_selection_sort(collection: list) -> list:
6470
6571
>>> reverse_selection_sort([5, 4, 3, 2, 1])
6672
[1, 2, 3, 4, 5]
73+
74+
>>> reverse_selection_sort(["banana", "apple", "cherry"])
75+
['apple', 'banana', 'cherry']
76+
77+
>>> reverse_selection_sort([3.14, 1.5, 2.7])
78+
[1.5, 2.7, 3.14]
79+
80+
>>> reverse_selection_sort([1, "a"]) # doctest: +ELLIPSIS
81+
Traceback (most recent call last):
82+
...
83+
TypeError: ...
6784
"""
6885
n = len(collection)
6986
for i in range(n - 1):

‎tests/test_sorts.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from sorts.quick_sort_3_partition import three_way_radix_quicksort
4747
from sorts.recursive_insertion_sort import rec_insertion_sort
4848
from sorts.recursive_mergesort_array import merge
49+
from sorts.reverse_selection import reverse_selection_sort
4950
from sorts.reversort import reversort
5051
from sorts.selection_sort import selection_sort
5152
from sorts.shell_sort import shell_sort
@@ -86,6 +87,7 @@ def test_heap_sort() -> None:
8687
pancake_sort,
8788
patience_sort,
8889
quick_sort,
90+
reverse_selection_sort,
8991
reversort,
9092
selection_sort,
9193
shell_sort,
@@ -163,6 +165,7 @@ def test_rec_insertion_sort(case) -> None:
163165
odd_even_transposition,
164166
pancake_sort,
165167
patience_sort,
168+
reverse_selection_sort,
166169
reversort,
167170
selection_sort,
168171
shrink_shell_sort,

0 commit comments

Comments
 (0)