Skip to content

Commit e07503b

Browse files
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
1 parent 9fa1b20 commit e07503b

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, TypeVar
79

8-
def odd_even_sort(input_list: list) -> list:
10+
class Comparable(Protocol):
11+
def __lt__(self, other:Any, /)->bool: ...
12+
13+
T = TypeVar("T", bound="Comparable")
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
@@ -121,6 +121,7 @@ def test_sort_matches_builtin(sort, case) -> None:
121121
gnome_sort,
122122
insertion_sort,
123123
merge_sort,
124+
odd_even_sort,
124125
selection_sort,
125126
],
126127
ids=lambda f: f.__name__,

0 commit comments

Comments
 (0)