Skip to content
Open
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
60 changes: 60 additions & 0 deletions random_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Tiny demo of an intentionally random sorting algorithm.

This exposes `random_sort`, which performs a bogosort-style shuffle until the
sequence happens to be ordered. It is useful purely for experimentation and
should not be used for production sorting needs.
"""

from __future__ import annotations

import random
from typing import List, Sequence, TypeVar


T = TypeVar("T")


def is_sorted(values: Sequence[T]) -> bool:
"""Return True when the provided sequence is monotonically non-decreasing."""

return all(values[idx] <= values[idx + 1] for idx in range(len(values) - 1))


def random_sort(values: Sequence[T], max_attempts: int = 10000) -> List[T]:
"""Sort `values` by repeatedly shuffling until the list is ordered.

Args:
values: Any finite sequence of comparable items.
max_attempts: Upper bound on the number of shuffles; the function raises
`RuntimeError` once this threshold is exceeded.

Returns:
A new list containing the items from `values` arranged in ascending
order.

Raises:
RuntimeError: If the data could not be sorted within `max_attempts`
shuffles.
"""

attempt = 0
candidate = list(values)
while attempt < max_attempts:
if is_sorted(candidate):
return candidate
random.shuffle(candidate)
attempt += 1

raise RuntimeError(
"Failed to randomly sort values within the allotted shuffle budget."
)


if __name__ == "__main__":
SAMPLE = [3, 1, 2]
try:
result = random_sort(SAMPLE)
except RuntimeError as exc: # pragma: no cover - demonstration only
print(f"Sorting failed: {exc}")
else:
print(f"Sorted output: {result}")
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const MapWrapper = styled.div`

const customIcon = new Icon({
iconUrl:
"https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png",
"https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png",
shadowUrl:
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png",
iconSize: [25, 41],
Expand Down