Skip to content
Closed
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
14 changes: 11 additions & 3 deletions sorts/reversort.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
python reversort.py
"""

from typing import Any
from typing import Protocol


def reversort(collection: list[Any]) -> list[Any]:
class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...


def reversort[T: Comparable](collection: list[T]) -> list[T]:
"""
Sort a list using the Reversort algorithm.

Expand Down Expand Up @@ -70,6 +74,10 @@ def reversort(collection: list[Any]) -> list[Any]:
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
>>> reversort([1, 3.3, 5, 7.7, 2, 4.4, 6])
[1, 2, 3.3, 4.4, 5, 6, 7.7]
>>> reversort([1, "a"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> import random
>>> collection_arg = random.sample(range(-50, 50), 100)
>>> reversort(collection_arg) == sorted(collection_arg)
Expand All @@ -96,7 +104,7 @@ def reversort(collection: list[Any]) -> list[Any]:
return arr


def reversort_cost(collection: list[Any]) -> int:
def reversort_cost[T: Comparable](collection: list[T]) -> int:
"""
Calculate the cost of sorting using Reversort.

Expand Down
Loading