Skip to content

Commit eb62b14

Browse files
committed
sorts: make pancake_sort generic over Comparable items
Part of #15234 - switch pancake_sort's TypeVar to the Comparable/TypeVar-bound pattern used by insertion_sort.py - add doctests covering strings, floats, and random data - register pancake_sort in tests/test_sorts.py's shared battery and the non-comparable-items rejection test
1 parent 7b5e704 commit eb62b14

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

sorts/pancake_sort.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
This is a pure Python implementation of the pancake sort algorithm
3+
34
For doctests run following command:
45
python3 -m doctest -v pancake_sort.py
56
or
@@ -9,23 +10,42 @@
910
"""
1011

1112
from collections.abc import Sequence
12-
from typing import TypeVar
13+
from typing import Any, Protocol, TypeVar
14+
15+
16+
class Comparable(Protocol):
17+
def __lt__(self, other: Any, /) -> bool: ...
18+
1319

14-
T = TypeVar("T")
20+
T = TypeVar("T", bound=Comparable)
1521

1622

17-
def pancake_sort[T](arr: Sequence[T]) -> list[T]:
23+
def pancake_sort[T: Comparable](arr: Sequence[T]) -> list[T]:
1824
"""Sort Array with Pancake Sort.
19-
:param arr: Collection containing comparable items
20-
:return: Collection ordered in ascending order of items
25+
26+
:param arr: some ordered collection with heterogeneous comparable items
27+
inside
28+
:return: the same collection ordered by ascending
29+
2130
Examples:
2231
>>> pancake_sort([0, 5, 3, 2, 2])
2332
[0, 2, 2, 3, 5]
2433
>>> pancake_sort([])
2534
[]
2635
>>> pancake_sort([-2, -5, -45])
2736
[-45, -5, -2]
37+
>>> pancake_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
38+
True
39+
>>> import random
40+
>>> collection = random.sample(range(-50, 50), 100)
41+
>>> pancake_sort(collection) == sorted(collection)
42+
True
43+
>>> import string
44+
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
45+
>>> pancake_sort(collection) == sorted(collection)
46+
True
2847
"""
48+
arr = list(arr)
2949
cur = len(arr)
3050
while cur > 1:
3151
# Find the maximum number in arr
@@ -39,6 +59,10 @@ def pancake_sort[T](arr: Sequence[T]) -> list[T]:
3959

4060

4161
if __name__ == "__main__":
62+
from doctest import testmod
63+
64+
testmod()
65+
4266
user_input = input("Enter numbers separated by a comma:\n").strip()
4367
unsorted = [int(item) for item in user_input.split(",")]
4468
print(pancake_sort(unsorted))

tests/test_sorts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from sorts.iterative_merge_sort import iter_merge_sort
3434
from sorts.merge_sort import merge_sort
3535
from sorts.odd_even_sort import odd_even_sort
36+
from sorts.pancake_sort import pancake_sort
3637
from sorts.patience_sort import patience_sort
3738
from sorts.quick_sort import quick_sort
3839
from sorts.selection_sort import selection_sort
@@ -64,6 +65,7 @@ def test_heap_sort() -> None:
6465
iter_merge_sort,
6566
merge_sort,
6667
odd_even_sort,
68+
pancake_sort,
6769
patience_sort,
6870
quick_sort,
6971
selection_sort,
@@ -121,6 +123,7 @@ def test_sort_matches_builtin(sort, case) -> None:
121123
gnome_sort,
122124
insertion_sort,
123125
merge_sort,
126+
pancake_sort,
124127
selection_sort,
125128
],
126129
ids=lambda f: f.__name__,

0 commit comments

Comments
 (0)