sorts.bogo_sort¶
This is a pure Python implementation of the bogosort algorithm, also known as permutation sort, stupid sort, slowsort, shotgun sort, or monkey sort. Bogosort generates random permutations until it guesses the correct one.
More info on: https://en.wikipedia.org/wiki/Bogosort
For doctests run following command: python -m doctest -v bogo_sort.py or python3 -m doctest -v bogo_sort.py For manual testing run: python bogo_sort.py
Attributes¶
Classes¶
Base class for protocol classes. |
Functions¶
|
Pure implementation of the bogosort algorithm in Python |
Module Contents¶
- class sorts.bogo_sort.Comparable¶
Bases:
ProtocolBase 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¶
- sorts.bogo_sort.bogo_sort[T: Comparable](collection: list[T]) list[T]¶
Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending Examples: >>> bogo_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> bogo_sort([]) [] >>> bogo_sort([-2, -5, -45]) [-45, -5, -2] >>> bogo_sort([“c”, “a”, “b”]) [‘a’, ‘b’, ‘c’] >>> bogo_sort([2.5, -1.0, 0.0]) [-1.0, 0.0, 2.5] >>> bogo_sort([1, “a”]) Traceback (most recent call last): … TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
- sorts.bogo_sort.user_input¶