From d0d5bdd2145c5ae2c1e404c8e9c0a6a6a3ca98fc Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Wed, 9 Sep 2026 14:56:49 +0530 Subject: [PATCH 1/4] Improve binary insertion sort comparable typing --- sorts/binary_insertion_sort.py | 14 +++++++++++++- tests/test_sorts.py | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index b928316a849d..90ec768391ed 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -10,8 +10,16 @@ python binary_insertion_sort.py """ +from typing import Protocol, TypeVar -def binary_insertion_sort(collection: list) -> list: + +class Comparable(Protocol): + def __lt__(self, other: object, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + +def binary_insertion_sort(collection: list[T]) -> list[T]: """ Sorts a list using the binary insertion sort algorithm. @@ -36,6 +44,10 @@ def binary_insertion_sort(collection: list) -> list: >>> collection = random.choices(string.ascii_letters + string.digits, k=100) >>> binary_insertion_sort(collection) == sorted(collection) True + >>> binary_insertion_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ n = len(collection) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index e24f177cf72c..6de12789dd0a 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -87,3 +87,8 @@ def test_heap_sort(): def test_sort_matches_builtin(sort, case): """Each sort must reproduce the ordering of the built-in ``sorted``.""" assert list(sort(list(case))) == sorted(case) + + +def test_binary_insertion_sort_rejects_non_comparable_items(): + with pytest.raises(TypeError): + binary_insertion_sort([1, "a"]) From d81f4d08188ba77dd4a24e347c9b7982a2634a87 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Sep 2026 10:50:30 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/binary_insertion_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index 90ec768391ed..c04a3eed0147 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -19,6 +19,7 @@ def __lt__(self, other: object, /) -> bool: ... T = TypeVar("T", bound=Comparable) + def binary_insertion_sort(collection: list[T]) -> list[T]: """ Sorts a list using the binary insertion sort algorithm. From bf627dc5bd43975f1fa25806ef6fd710cd9beb14 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 9 Sep 2026 15:19:58 +0200 Subject: [PATCH 3/4] Update binary_insertion_sort function signature --- sorts/binary_insertion_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py index c04a3eed0147..f974a97f316e 100644 --- a/sorts/binary_insertion_sort.py +++ b/sorts/binary_insertion_sort.py @@ -20,7 +20,7 @@ def __lt__(self, other: object, /) -> bool: ... T = TypeVar("T", bound=Comparable) -def binary_insertion_sort(collection: list[T]) -> list[T]: +def binary_insertion_sort[T: Comparable](collection: list[T]) -> list[T]: """ Sorts a list using the binary insertion sort algorithm. From 33dd8c0a492077895bea95742980599c786f09ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Sep 2026 13:36:26 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 2df5c3924a8d..157532aab98d 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -49,7 +49,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: for k in range(low, high + 1): array[k] = aux[k] - print(f"After merge: {array[low:high + 1]}") + print(f"After merge: {array[low : high + 1]}") # Example usage