From 032afb52deb153c1d30e22432925b4f359b67aa9 Mon Sep 17 00:00:00 2001 From: 0x <129736887+itsnevu@users.noreply.github.com> Date: Fri, 18 Sep 2026 07:39:28 +0700 Subject: [PATCH] sorts: type slowsort for any comparable items (#15347) --- sorts/slowsort.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/sorts/slowsort.py b/sorts/slowsort.py index 394e6eed5..691fa7774 100644 --- a/sorts/slowsort.py +++ b/sorts/slowsort.py @@ -11,8 +11,16 @@ Source: https://en.wikipedia.org/wiki/Slowsort from __future__ import annotations +from typing import Protocol -def slowsort(sequence: list, start: int | None = None, end: int | None = None) -> None: + +class Comparable(Protocol): + def __lt__(self, other: object, /) -> bool: ... + + +def slowsort[T: Comparable]( + sequence: list[T], start: int | None = None, end: int | None = None +) -> None: """ Sorts sequence[start..end] (both inclusive) in-place. start defaults to 0 if not given. @@ -20,6 +28,14 @@ def slowsort(sequence: list, start: int | None = None, end: int | None = None) - It returns None. >>> seq = [1, 6, 2, 5, 3, 4, 4, 5]; slowsort(seq); seq [1, 2, 3, 4, 4, 5, 5, 6] + >>> seq = ["c", "a", "b"]; slowsort(seq); seq + ['a', 'b', 'c'] + >>> seq = [2.5, -1, 0.0]; slowsort(seq); seq + [-1, 0.0, 2.5] + >>> slowsort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' >>> seq = []; slowsort(seq); seq [] >>> seq = [2]; slowsort(seq); seq