sorts.patience_sort

Attributes

user_input

Classes

Comparable

Base class for protocol classes.

Stack

Built-in mutable sequence.

Functions

patience_sort(→ list[T])

A pure implementation of patience sort algorithm in Python

Module Contents

class sorts.patience_sort.Comparable

Bases: Protocol

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).

For example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto[T](Protocol):
    def meth(self) -> T:
        ...
__lt__(other: object, /) → bool
class sorts.patience_sort.Stack[T: Comparable]

Bases: list[T]

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

__eq__(other: object) → bool

Return self==value.

__lt__(other: Stack[T]) → bool

Return self<value.

sorts.patience_sort.patience_sort[T: Comparable](collection: list[T]) → list[T]

A pure implementation of patience sort algorithm in Python

Parameters:

collection – some mutable ordered collection with heterogeneous

comparable items inside :return: the same collection ordered by ascending

Examples: >>> patience_sort([1, 9, 5, 21, 17, 6]) [1, 5, 6, 9, 17, 21]

>>> patience_sort([])
[]
>>> patience_sort([-3, -17, -48])
[-48, -17, -3]
>>> patience_sort(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
True
>>> patience_sort([3.3, 1.1, 2.2])
[1.1, 2.2, 3.3]
>>> patience_sort([1, "a"])
Traceback (most recent call last):
    ...
TypeError: '<' not supported between instances of 'str' and 'int'
sorts.patience_sort.user_input