From e07503b2536b83d2c12d5adb73221ab0393373dc Mon Sep 17 00:00:00 2001 From: aayushgupta7725 Date: Thu, 17 Sep 2026 20:28:14 +0530 Subject: [PATCH 1/5] 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 --- sorts/odd_even_sort.py | 31 +++++++++++++++++++++++-------- tests/test_sorts.py | 1 + 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index 7dfe03054bc3..e6cec8e7f607 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -4,8 +4,15 @@ https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort """ +from collections.abc import MutableSequence +from typing import Any, Protocol, TypeVar -def odd_even_sort(input_list: list) -> list: +class Comparable(Protocol): + def __lt__(self, other:Any, /)->bool: ... + +T = TypeVar("T", bound="Comparable") + +def odd_even_sort[T:Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ Sort input with odd even sort. @@ -24,22 +31,30 @@ def odd_even_sort(input_list: list) -> list: [-10, -1, 2, 10] >>> odd_even_sort([1 ,2 ,3 ,4]) [1, 2, 3, 4] + >>> odd_even_sort(["c","a","b"]) + ['a', 'b', 'c'] + >>> odd_even_sort([2.5, -1, 0.0]) + [-1, 0.0, 2.5] + >>> odd_even_sort([1,"a"]) + Traceback (most recent call last): + ... + TypeError: '>' not supported between instances of 'int' and 'str' """ is_sorted = False while is_sorted is False: # Until all the indices are traversed keep looping is_sorted = True - for i in range(0, len(input_list) - 1, 2): # iterating over all even indices - if input_list[i] > input_list[i + 1]: - input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i] + for i in range(0, len(collection) - 1, 2): # iterating over all even indices + if collection[i] > collection[i + 1]: + collection[i], collection[i + 1] = collection[i + 1], collection[i] # swapping if elements not in order is_sorted = False - for i in range(1, len(input_list) - 1, 2): # iterating over all odd indices - if input_list[i] > input_list[i + 1]: - input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i] + for i in range(1, len(collection) - 1, 2): # iterating over all odd indices + if collection[i] > collection[i + 1]: + collection[i], collection[i + 1] = collection[i + 1], collection[i] # swapping if elements not in order is_sorted = False - return input_list + return collection if __name__ == "__main__": diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 2c9b79aa4bfe..fd4f5605ab45 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -121,6 +121,7 @@ def test_sort_matches_builtin(sort, case) -> None: gnome_sort, insertion_sort, merge_sort, + odd_even_sort, selection_sort, ], ids=lambda f: f.__name__, From e3996bb49fa77d42e565f4ec6f11e2b9baf1a15b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 18 Sep 2026 13:46:23 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/odd_even_sort.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index e6cec8e7f607..b641c7cc79b9 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -7,12 +7,15 @@ from collections.abc import MutableSequence from typing import Any, Protocol, TypeVar + class Comparable(Protocol): - def __lt__(self, other:Any, /)->bool: ... + def __lt__(self, other: Any, /) -> bool: ... + T = TypeVar("T", bound="Comparable") -def odd_even_sort[T:Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: + +def odd_even_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ Sort input with odd even sort. From c0575fec70f029bb7b387028ecb448454f660cc3 Mon Sep 17 00:00:00 2001 From: aayushgupta7725 Date: Sat, 19 Sep 2026 09:56:27 +0530 Subject: [PATCH 3/5] Drop redundant module-level TypeVar, bind Comparable to __gt__ --- sorts/odd_even_sort.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index b641c7cc79b9..b70555b9e4fd 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -5,15 +5,10 @@ """ from collections.abc import MutableSequence -from typing import Any, Protocol, TypeVar - +from typing import Any, Protocol class Comparable(Protocol): - def __lt__(self, other: Any, /) -> bool: ... - - -T = TypeVar("T", bound="Comparable") - + def __gt__(self, other: Any, /) -> bool: ... def odd_even_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ From d47dab12870004873d8971af340943783bbdbc88 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Sep 2026 04:26:45 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/odd_even_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index b70555b9e4fd..ce1aae98ae39 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -7,9 +7,11 @@ from collections.abc import MutableSequence from typing import Any, Protocol + class Comparable(Protocol): def __gt__(self, other: Any, /) -> bool: ... + def odd_even_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]: """ Sort input with odd even sort. From 961cd45e9382e7aea23b08bab8ca9e7388885c25 Mon Sep 17 00:00:00 2001 From: aayushgupta7725 Date: Sat, 19 Sep 2026 10:14:13 +0530 Subject: [PATCH 5/5] Fix import block formatting per ruff --- sorts/odd_even_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index b70555b9e4fd..52fc1b6067a5 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -7,6 +7,7 @@ from collections.abc import MutableSequence from typing import Any, Protocol + class Comparable(Protocol): def __gt__(self, other: Any, /) -> bool: ...