-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleep_sort.py
More file actions
56 lines (40 loc) · 1.25 KB
/
Copy pathsleep_sort.py
File metadata and controls
56 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import asyncio
def __adjust_sleep(t):
"""Modifier to scale down seconds
Args:
t: seconds of sleep
Returns:
An adjusted time for sleep sort.
For example 5 seconds can easily become .0005 seconds.
"""
return float(t) / float(10000)
async def __sleep(delay, f= __adjust_sleep):
"""Asychronous sleep function wrapper
Args:
delay: number of seconds intended for the sleep.
f: a function to modify delay amount.
Returns:
'delay' after having slept f(delay) seconds.
"""
await asyncio.sleep(f(delay))
return delay
async def __sleep_sort(values):
"""Sorts using asycio sleep function
Args:
values: list of ints or floats
Returns:
A list of ints or floats sorted from least to greatest.
"""
snoozes = [__sleep(duration) for duration in values]
woke_values = []
for value in asyncio.as_completed(snoozes):
woke_values.append(await value)
return woke_values
def sleep_sort(list_of_values):
"""Sorts using asycio sleep function
Args:
list_of_values: list of ints or floats
Returns:
A list of ints or floats sorted from least to greatest.
"""
return asyncio.run(__sleep_sort(list_of_values))