From ace0391bbf5684f78e4a5d2ef660803af9057ecd Mon Sep 17 00:00:00 2001 From: brighttsekumah46-coder Date: Thu, 17 Sep 2026 13:45:20 +0000 Subject: [PATCH 1/3] Add unit tests for comparable items and TypeError in slowsort --- tests/test_sorts.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 2c9b79aa4bfe..a74fa2b3cecc 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -128,3 +128,23 @@ def test_sort_matches_builtin(sort, case) -> None: def test_sort_rejects_non_comparable_items(sort) -> None: with pytest.raises(TypeError): sort([1, "a"]) +import pytest +from sorts.slowsort import slowsort + + +def test_slowsort_comparable_types(): + # Test with strings + words = ["banana", "apple", "cherry", "date"] + slowsort(words) + assert words == ["apple", "banana", "cherry", "date"] + + # Test with floats and negative numbers + floats = [3.14, -1.0, 2.5, 0.0] + slowsort(floats) + assert floats == [-1.0, 0.0, 2.5, 3.14] + + +def test_slowsort_type_error_on_incomparable(): + mixed = [1, "a"] + with pytest.raises(TypeError): + slowsort(mixed) \ No newline at end of file From 34d83ef46778cbe7822ce32b6d608104a8413987 Mon Sep 17 00:00:00 2001 From: Essuman Isaac Date: Wed, 23 Sep 2026 23:12:36 +0000 Subject: [PATCH 2/3] Add Comparable protocol and update slowsort type annotations --- sorts/slowsort.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/sorts/slowsort.py b/sorts/slowsort.py index 394e6eed50b1..c34fd76c86b8 100644 --- a/sorts/slowsort.py +++ b/sorts/slowsort.py @@ -11,8 +11,21 @@ from __future__ import annotations +from typing import Protocol, TypeVar + + +class Comparable(Protocol): + + def __lt__(self, other: object, /) -> bool: + ... +def slowsort( + sequence: list[T], start: int | None = None, end: int | None = None +) -> None: + +T = TypeVar("T", bound=Comparable) +def slowsort(sequence: list[T], start: int | None = None, end: int | None = None) -> None: + -def slowsort(sequence: list, start: int | None = None, end: int | None = None) -> None: """ Sorts sequence[start..end] (both inclusive) in-place. start defaults to 0 if not given. From 8ccf14b948a6a2fd502d07cf9c754c5f4464941c Mon Sep 17 00:00:00 2001 From: Freda Ofori <258169878+fredaesiofori@users.noreply.github.com> Date: Wed, 23 Sep 2026 23:34:36 +0000 Subject: [PATCH 3/3] Add doctests for comparable types and format slowsort.py --- sorts/slowsort.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sorts/slowsort.py b/sorts/slowsort.py index c34fd76c86b8..83ba59496d63 100644 --- a/sorts/slowsort.py +++ b/sorts/slowsort.py @@ -18,14 +18,14 @@ class Comparable(Protocol): def __lt__(self, other: object, /) -> bool: ... -def slowsort( - sequence: list[T], start: int | None = None, end: int | None = None -) -> None: + T = TypeVar("T", bound=Comparable) -def slowsort(sequence: list[T], start: int | None = None, end: int | None = None) -> None: +def slowsort( + 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. @@ -47,6 +47,10 @@ def slowsort(sequence: list[T], start: int | None = None, end: int | None = None [5, 6, 7, 8, 9, 4, 3, 2, 1, 0] >>> seq = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(seq, start = 5); seq [9, 8, 7, 6, 5, 0, 1, 2, 3, 4] + >>> seq = ["cherry", "banana", "apple", "date"]; slowsort(seq); seq + ['apple', 'banana', 'cherry', 'date'] + >>> seq = [3.14, -1.0, 2.5, 0.0]; slowsort(seq); seq + [-1.0, 0.0, 2.5, 3.14] """ if start is None: start = 0 @@ -71,4 +75,4 @@ def slowsort(sequence: list[T], start: int | None = None, end: int | None = None if __name__ == "__main__": from doctest import testmod - testmod() + testmod() \ No newline at end of file