-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank.py
34 lines (27 loc) · 1.17 KB
/
rank.py
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
from heapq import heappop, heappush
suffix, race, finished_race = {1:"st",2:"nd",3:"rd"}, [], []
def print_ranking():
if not finished_race: print("not ranked yet")
print(f"Comparing {len(finished_race)} algorithms")
for my_rank in finished_race:
rank, name, time = my_rank.position, my_rank.name, my_rank.exec_time
print(f"{name}\t took {time:.4f} seconds\t &\t placed {rank}{suffix.get(rank,'th')}")
class rank_sort:
def __init__(self, name: str):
self.name, self.position, self.exec_time = name, None, float('inf')
def __lt__(self, other):
return self.exec_time < other.exec_time
def __repr__(self) -> str:
position, name = self.position, self.name
if not position: return f"{name} still racing..."
return f"{name} ranked {position}{suffix.get(position,'th')}"
def report_time(self, time: float):
self.exec_time = time
heappush(race, self)
@staticmethod
def end_ranking():
for position in range(len(race)):
my_rank, rank = heappop(race), position+1
my_rank.position = rank
finished_race.append(my_rank)
print_ranking()