From d49a112a1c31824d121507e04649c734a5ca642d Mon Sep 17 00:00:00 2001 From: Orji Patricia <81320550+otrisha@users.noreply.github.com> Date: Sat, 12 Sep 2026 02:06:21 +0100 Subject: [PATCH 1/5] sorts: make comb sort generic for comparable items --- sorts/comb_sort.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 72caeb9c7350..cfa6539aefac 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -18,8 +18,14 @@ python comb_sort.py """ +from typing import Any, Protocol -def comb_sort(data: list) -> list: + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +def comb_sort[T: Comparable](data: list[T]) -> list[T]: """Pure implementation of comb sort algorithm in Python :param data: mutable collection with comparable items :return: the same collection in ascending order @@ -32,6 +38,14 @@ def comb_sort(data: list) -> list: [-15, -7, 0, 2, 3, 8, 45, 99] >>> comb_sort([2, 0, 3, 4, 5, 6, 1]) [0, 1, 2, 3, 4, 5, 6] + >>> comb_sort(["c", "a", "b"]) + ['a', 'b', 'c'] + >>> comb_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] + >>> comb_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ shrink_factor = 1.3 gap = len(data) @@ -47,7 +61,7 @@ def comb_sort(data: list) -> list: index = 0 while index + gap < len(data): - if data[index] > data[index + gap]: + if data[index + gap] < data[index]: # Swap values data[index], data[index + gap] = data[index + gap], data[index] completed = False From 8b9fbab0400d9789095a31b6ed65ecf035202709 Mon Sep 17 00:00:00 2001 From: Orji Patricia <81320550+otrisha@users.noreply.github.com> Date: Sat, 12 Sep 2026 02:06:34 +0100 Subject: [PATCH 2/5] tests: cover comb sort incomparable inputs --- tests/test_sorts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 799ab5979dff..adabc2c7d43a 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -116,6 +116,7 @@ def test_sort_matches_builtin(sort, case): bubble_sort_recursive, circle_sort, cocktail_shaker_sort, + comb_sort, gnome_sort, insertion_sort, merge_sort, From fff2fb7b3e99b07a99cf00af6f072ec3b5dc48f3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 12 Sep 2026 07:45:23 +0200 Subject: [PATCH 3/5] Update sorts/comb_sort.py --- sorts/comb_sort.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index cfa6539aefac..1d7c90de87ea 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -40,7 +40,9 @@ def comb_sort[T: Comparable](data: list[T]) -> list[T]: [0, 1, 2, 3, 4, 5, 6] >>> comb_sort(["c", "a", "b"]) ['a', 'b', 'c'] - >>> comb_sort([2.5, -1.0, 0.0]) + >>> comb_sort("cab") + ['a', 'b', 'c'] + >>> comb_sort([2.5, -1, 0.0]) [-1.0, 0.0, 2.5] >>> comb_sort([1, "a"]) Traceback (most recent call last): From a5c73c814b5b3ff1d3a40059a6bbda283c1b7a82 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 12 Sep 2026 07:49:12 +0200 Subject: [PATCH 4/5] Apply suggestion from @cclauss --- sorts/comb_sort.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 1d7c90de87ea..179d1e74bbed 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -40,8 +40,6 @@ def comb_sort[T: Comparable](data: list[T]) -> list[T]: [0, 1, 2, 3, 4, 5, 6] >>> comb_sort(["c", "a", "b"]) ['a', 'b', 'c'] - >>> comb_sort("cab") - ['a', 'b', 'c'] >>> comb_sort([2.5, -1, 0.0]) [-1.0, 0.0, 2.5] >>> comb_sort([1, "a"]) From b43b35835665093a775ff204d5accab751bcf762 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 12 Sep 2026 07:51:44 +0200 Subject: [PATCH 5/5] Apply suggestion from @cclauss --- sorts/comb_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 179d1e74bbed..46f6c77c5be7 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -41,7 +41,7 @@ def comb_sort[T: Comparable](data: list[T]) -> list[T]: >>> comb_sort(["c", "a", "b"]) ['a', 'b', 'c'] >>> comb_sort([2.5, -1, 0.0]) - [-1.0, 0.0, 2.5] + [-1, 0.0, 2.5] >>> comb_sort([1, "a"]) Traceback (most recent call last): ...