Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions sorts/slowsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

from __future__ import annotations

from typing import Protocol
from typing import Protocol, TypeVar


class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...


def slowsort[T: Comparable](
T = TypeVar("T", bound=Comparable)


def slowsort(
sequence: list[T], start: int | None = None, end: int | None = None
) -> None:

Check failure on line 26 in sorts/slowsort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP047)

sorts/slowsort.py:24:5: UP047 Generic function `slowsort` should use type parameters help: Use type parameters
"""
Sorts sequence[start..end] (both inclusive) in-place.
start defaults to 0 if not given.
Expand All @@ -28,14 +31,6 @@
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
Expand All @@ -50,6 +45,10 @@
[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
Expand Down
22 changes: 22 additions & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,28 @@
sort([1, "a"])


import pytest

Check failure on line 221 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E402)

tests/test_sorts.py:221:1: E402 Module level import not at top of file help: Move module level imports to top of file
from sorts.slowsort import slowsort

Check failure on line 222 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E402)

tests/test_sorts.py:222:1: E402 Module level import not at top of file help: Move module level imports to top of file

Check failure on line 222 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

tests/test_sorts.py:221:1: I001 Import block is un-sorted or un-formatted help: Organize imports


def test_slowsort_comparable_types():

Check failure on line 225 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (ANN201)

tests/test_sorts.py:225:5: ANN201 Missing return type annotation for public function `test_slowsort_comparable_types` help: Add return type annotation: `None`
# 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():

Check failure on line 237 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (ANN201)

tests/test_sorts.py:237:5: ANN201 Missing return type annotation for public function `test_slowsort_type_error_on_incomparable` help: Add return type annotation: `None`
mixed = [1, "a"]
with pytest.raises(TypeError):
slowsort(mixed)


def test_rec_insertion_sort_rejects_non_comparable_items() -> None:
with pytest.raises(TypeError):
rec_insertion_sort([1, "a"], 2)
Expand Down
Loading