Skip to content

Commit bb40379

Browse files
author
nightcityblade
committed
types(power_sort): preserve input item type
1 parent 32e2d50 commit bb40379

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

‎sorts/power_sort.py‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727

2828
from __future__ import annotations
2929

30-
from collections.abc import Callable
30+
from collections.abc import Callable, Iterable
3131
from typing import Any
3232

3333

34-
def _find_run(
35-
arr: list, start: int, end: int, key: Callable[[Any], Any] | None = None
34+
def _find_run[T](
35+
arr: list[T], start: int, end: int, key: Callable[[Any], Any] | None = None
3636
) -> int:
3737
"""
3838
Detect a run (ascending or descending sequence) starting at 'start'.
@@ -134,8 +134,8 @@ def _node_power(total_length: int, b1: int, n1: int, b2: int, n2: int) -> int:
134134
return power
135135

136136

137-
def _merge(
138-
arr: list,
137+
def _merge[T](
138+
arr: list[T],
139139
start1: int,
140140
end1: int,
141141
end2: int,
@@ -196,12 +196,12 @@ def _merge(
196196
k += 1
197197

198198

199-
def power_sort(
200-
collection: list,
199+
def power_sort[T](
200+
collection: Iterable[T],
201201
*,
202202
key: Callable[[Any], Any] | None = None,
203203
reverse: bool = False,
204-
) -> list:
204+
) -> list[T]:
205205
"""
206206
Sort a list using the PowerSort algorithm.
207207
@@ -243,6 +243,8 @@ def power_sort(
243243
['apple', 'banana', 'cherry']
244244
>>> power_sort([3.14, 2.71, 1.41, 1.73])
245245
[1.41, 1.73, 2.71, 3.14]
246+
>>> power_sort(value for value in [3, 1, 2])
247+
[1, 2, 3]
246248
>>> power_sort([5, 2, 8, 1, 9], reverse=True)
247249
[9, 8, 5, 2, 1]
248250
>>> power_sort(['apple', 'pie', 'a', 'longer'], key=len)
@@ -257,12 +259,16 @@ def power_sort(
257259
>>> result = power_sort(list(reversed(range(50))))
258260
>>> result == list(range(50))
259261
True
262+
>>> power_sort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
263+
Traceback (most recent call last):
264+
...
265+
TypeError: '<' not supported between instances of 'str' and 'int'
260266
"""
261-
if len(collection) <= 1:
262-
return collection
263-
264-
# Make a copy to avoid modifying the original if it's immutable
267+
# Make a copy so any iterable is accepted and the original is not modified.
265268
arr = list(collection)
269+
if len(arr) <= 1:
270+
return arr
271+
266272
total_length = len(arr)
267273

268274
# Adjust key function for reverse sorting

‎tests/test_sorts.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from sorts.odd_even_transposition_single_threaded import odd_even_transposition
4545
from sorts.pancake_sort import pancake_sort
4646
from sorts.patience_sort import patience_sort
47+
from sorts.power_sort import power_sort
4748
from sorts.quick_sort import quick_sort
4849
from sorts.quick_sort_3_partition import three_way_radix_quicksort
4950
from sorts.recursive_insertion_sort import rec_insertion_sort
@@ -117,6 +118,7 @@ def test_intro_sort_heap_fallback_preserves_surrounding_items(max_depth: int) ->
117118
odd_even_transposition,
118119
pancake_sort,
119120
patience_sort,
121+
power_sort,
120122
quick_sort,
121123
recursive_quick_sort,
122124
reverse_selection_sort,
@@ -198,6 +200,7 @@ def test_rec_insertion_sort(case) -> None:
198200
odd_even_transposition,
199201
pancake_sort,
200202
patience_sort,
203+
power_sort,
201204
recursive_quick_sort,
202205
reverse_selection_sort,
203206
reversort,

0 commit comments

Comments
 (0)