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
43 changes: 43 additions & 0 deletions sorts/sleep_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# sorts/sleep_sort.py
from __future__ import annotations

import threading
import time
from typing import List

Check failure on line 6 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

sorts/sleep_sort.py:6:1: UP035 `typing.List` is deprecated, use `list` instead


def sleep_sort(arr: List[int]) -> List[int]:

Check failure on line 9 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:9:35: UP006 Use `list` instead of `List` for type annotation

Check failure on line 9 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:9:21: UP006 Use `list` instead of `List` for type annotation
"""
"Sorts" a list by sleeping for each value's duration.
Fun demo – **not** for production!

Check failure on line 12 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

sorts/sleep_sort.py:12:14: RUF002 Docstring contains ambiguous `–` (EN DASH). Did you mean `-` (HYPHEN-MINUS)?

>>> import random; random.seed(42)
>>> sleep_sort([3, 1, 4, 1, 5])
[1, 1, 3, 4, 5]
>>> sleep_sort([])
[]
>>> sleep_sort([42])
[42]
"""
if not arr:
return []

result: List[int] = []

Check failure on line 25 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:25:13: UP006 Use `list` instead of `List` for type annotation

def sleeper(value: int) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/sleep_sort.py, please provide doctest for the function sleeper

time.sleep(value / 1000.0) # scale down for fast tests
result.append(value)

threads = [threading.Thread(target=sleeper, args=(x,)) for x in arr]
for thread in threads:
thread.start()
for thread in threads:
thread.join()

return result


if __name__ == "__main__":
import doctest

doctest.testmod(verbose=True)
Loading