From e56bff417c52fcbbcca502716e73b720cea72274 Mon Sep 17 00:00:00 2001 From: Harsh Raj Singhania <40535627+HarshRajSinghania@users.noreply.github.com> Date: Thu, 10 Sep 2026 00:09:40 +0530 Subject: [PATCH 1/2] sorts: type gnome sort for comparable items --- sorts/gnome_sort.py | 15 +++++++++++++-- tests/test_sorts.py | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sorts/gnome_sort.py b/sorts/gnome_sort.py index 3002bc6a8b18..e06813e8c1eb 100644 --- a/sorts/gnome_sort.py +++ b/sorts/gnome_sort.py @@ -12,8 +12,14 @@ python3 gnome_sort.py """ +from typing import Protocol -def gnome_sort(lst: list) -> list: + +class Comparable(Protocol): + def __lt__(self, other: object, /) -> bool: ... + + +def gnome_sort[T: Comparable](lst: list[T]) -> list[T]: """ Pure implementation of the gnome sort algorithm in Python @@ -32,7 +38,12 @@ def gnome_sort(lst: list) -> list: >>> "".join(gnome_sort(list(set("Gnomes are stupid!")))) ' !Gadeimnoprstu' - """ + + >>> gnome_sort(["d", "a", "c", "b"]) + ['a', 'b', 'c', 'd'] + >>> gnome_sort([2.5, -1.0, 0.0]) + [-1.0, 0.0, 2.5] +""" if len(lst) <= 1: return lst diff --git a/tests/test_sorts.py b/tests/test_sorts.py index caa4b31cac81..df893ddd7637 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -96,6 +96,7 @@ def test_sort_matches_builtin(sort, case): bubble_sort_iterative, bubble_sort_recursive, insertion_sort, + gnome_sort, ], ids=lambda f: f.__name__, ) From 13eaa82ff6da803f48d3570591d916673ea19cd6 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 18:40:26 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/gnome_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/gnome_sort.py b/sorts/gnome_sort.py index e06813e8c1eb..ad747369f8e4 100644 --- a/sorts/gnome_sort.py +++ b/sorts/gnome_sort.py @@ -43,7 +43,7 @@ def gnome_sort[T: Comparable](lst: list[T]) -> list[T]: ['a', 'b', 'c', 'd'] >>> gnome_sort([2.5, -1.0, 0.0]) [-1.0, 0.0, 2.5] -""" + """ if len(lst) <= 1: return lst