Skip to content
Closed
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
25 changes: 13 additions & 12 deletions sorts/slowsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@

from __future__ import annotations

from typing import Protocol
from typing import Protocol, TypeVar


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

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 28 in sorts/slowsort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP047)

sorts/slowsort.py:26:5: UP047 Generic function `slowsort` should use type parameters help: Use type parameters

Check failure on line 28 in sorts/slowsort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP047)

sorts/slowsort.py:26: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 +33,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 +47,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 All @@ -74,4 +75,4 @@
if __name__ == "__main__":
from doctest import testmod

testmod()
testmod()

Check failure on line 78 in sorts/slowsort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W292)

sorts/slowsort.py:78:14: W292 No newline at end of file help: Add trailing newline

Check failure on line 78 in sorts/slowsort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W292)

sorts/slowsort.py:78:14: W292 No newline at end of file help: Add trailing newline
20 changes: 20 additions & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@
def test_sort_rejects_non_comparable_items(sort) -> None:
with pytest.raises(TypeError):
sort([1, "a"])
import pytest

Check failure on line 219 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E402)

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

Check failure on line 219 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E402)

tests/test_sorts.py:219: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 220 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E402)

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

Check failure on line 220 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

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

Check failure on line 220 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E402)

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

Check failure on line 220 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

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


def test_slowsort_comparable_types():

Check failure on line 223 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (ANN201)

tests/test_sorts.py:223:5: ANN201 Missing return type annotation for public function `test_slowsort_comparable_types` help: Add return type annotation: `None`

Check failure on line 223 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (ANN201)

tests/test_sorts.py:223: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 235 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (ANN201)

tests/test_sorts.py:235:5: ANN201 Missing return type annotation for public function `test_slowsort_type_error_on_incomparable` help: Add return type annotation: `None`

Check failure on line 235 in tests/test_sorts.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (ANN201)

tests/test_sorts.py:235: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:
Expand Down
Loading