blob: 2a34c0554b6eeb2f349774359442dbf9c6718ea2 (
plain) (
blame)
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
57
|
from time import time
from typing import Tuple
from random import shuffle
from dataclasses import dataclass
SECS_IN_MIN = 60
def _shuffle_two_lists(a: list, b: list) -> Tuple[list, list]:
indices = list(range(len(a)))
shuffle(indices)
a = [a[index] for index in indices]
b = [b[index] for index in indices]
return a, b
def _pick_from_list_after_sorting_other(a: list, b: list[int]) -> list:
indices = [i[0] for i in sorted(enumerate(b), key=lambda x: x[1])]
return [a[index] for index in indices]
def sample_list(indices: list, cost: list[int], count: int = None) -> list:
if count is None:
count = len(indices)
indices, cost = _shuffle_two_lists(indices, cost)
indices = _pick_from_list_after_sorting_other(indices, cost)[:count]
return indices
@dataclass
class Timer:
ref: float = None
running: bool = False
max_mins: float = 17.5
default: str = ("00", "00")
def __post_init__(self):
self.max_seconds = self.max_mins * SECS_IN_MIN
def start(self):
self.ref = time() + self.max_seconds
self.running = True
def stop(self):
self.ref = None
self.running = False
def get(self):
if not self.running:
return self.default
total_seconds = self.ref - time()
if total_seconds < 0:
self.stop()
return self.default
mins = f"{int(total_seconds // SECS_IN_MIN):02d}"
secs = f"{int(total_seconds % SECS_IN_MIN):02d}"
return mins, secs
|