diff --git a/sorts/gnome_sort.py b/sorts/gnome_sort.py index 3002bc6a8b18..ad747369f8e4 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,6 +38,11 @@ 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__, )