Skip to content
Merged
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
18 changes: 17 additions & 1 deletion sorts/slowsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,31 @@

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.
end defaults to len(sequence) - 1 if not given.
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 Down
Loading