Skip to content

Commit 219478d

Browse files
aayushgupta7725pre-commit-ci[bot]cclauss
authored
sorts: make odd_even_sort generic over any comparable type (Part of #15234) (#15378)
* sorts: make odd_even_sort generic over any comparable type (Part of #15234) Adds a Comparable-bound TypeVar (matching the pattern used in insertion_sort.py), doctests covering strings, floats, and the non-comparable TypeError case, and registers odd_even_sort in the shared test_sort_rejects_non_comparable_items test. Part of #15234 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Drop redundant module-level TypeVar, bind Comparable to __gt__ * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix import block formatting per ruff --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 09e8265 commit 219478d

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎sorts/odd_even_sort.py‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
55
"""
66

7+
from collections.abc import MutableSequence
8+
from typing import Any, Protocol
79

8-
def odd_even_sort(input_list: list) -> list:
10+
11+
class Comparable(Protocol):
12+
def __gt__(self, other: Any, /) -> bool: ...
13+
14+
15+
def odd_even_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]:
916
"""
1017
Sort input with odd even sort.
1118
@@ -24,22 +31,30 @@ def odd_even_sort(input_list: list) -> list:
2431
[-10, -1, 2, 10]
2532
>>> odd_even_sort([1 ,2 ,3 ,4])
2633
[1, 2, 3, 4]
34+
>>> odd_even_sort(["c","a","b"])
35+
['a', 'b', 'c']
36+
>>> odd_even_sort([2.5, -1, 0.0])
37+
[-1, 0.0, 2.5]
38+
>>> odd_even_sort([1,"a"])
39+
Traceback (most recent call last):
40+
...
41+
TypeError: '>' not supported between instances of 'int' and 'str'
2742
"""
2843
is_sorted = False
2944
while is_sorted is False: # Until all the indices are traversed keep looping
3045
is_sorted = True
31-
for i in range(0, len(input_list) - 1, 2): # iterating over all even indices
32-
if input_list[i] > input_list[i + 1]:
33-
input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i]
46+
for i in range(0, len(collection) - 1, 2): # iterating over all even indices
47+
if collection[i] > collection[i + 1]:
48+
collection[i], collection[i + 1] = collection[i + 1], collection[i]
3449
# swapping if elements not in order
3550
is_sorted = False
3651

37-
for i in range(1, len(input_list) - 1, 2): # iterating over all odd indices
38-
if input_list[i] > input_list[i + 1]:
39-
input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i]
52+
for i in range(1, len(collection) - 1, 2): # iterating over all odd indices
53+
if collection[i] > collection[i + 1]:
54+
collection[i], collection[i + 1] = collection[i + 1], collection[i]
4055
# swapping if elements not in order
4156
is_sorted = False
42-
return input_list
57+
return collection
4358

4459

4560
if __name__ == "__main__":

‎tests/test_sorts.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def test_sort_matches_builtin(sort, case) -> None:
126126
gnome_sort,
127127
insertion_sort,
128128
merge_sort,
129+
odd_even_sort,
129130
pancake_sort,
130131
selection_sort,
131132
shrink_shell_sort,

0 commit comments

Comments
 (0)