-
Notifications
You must be signed in to change notification settings - Fork 0
/
climbing_the_leaderboard.py
70 lines (53 loc) · 1.79 KB
/
climbing_the_leaderboard.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from utilities import get_random_list
from utilities import timeit
@timeit
def solve_ranked_pythonic(ranked, player):
player_rank = []
unique_sorted_rank = list(set(ranked))
unique_sorted_rank.sort()
i = 0
current_position = len(unique_sorted_rank) + 1
for score in player:
if current_position > 1:
while i < len(unique_sorted_rank) and score >= unique_sorted_rank[i]:
current_position -= 1
i += 1
player_rank.append(current_position)
return player_rank
@timeit
def solve_ranked_efficient(ranking, player):
player_rank = []
player_index = len(player) - 1
ranking_size = len(ranking)
ranking_index = 0
position = 1
while ranking_index < ranking_size and player_index >= 0:
current_player = player[player_index]
ranking_points = ranking[ranking_index]
if current_player >= ranking_points:
player_rank.append(position)
player_index -= 1
else:
ranking_index += 1
if ranking_index < ranking_size and ranking[ranking_index] < ranking[ranking_index - 1]:
position += 1
position += 1
while player_index >= 0:
player_rank.append(position)
player_index -= 1
return player_rank[::-1]
def test_exec_time():
ranked = get_random_list()
player = get_random_list()
solve_ranked_pythonic(ranked, player)
solve_ranked_efficient(ranked, player)
def test_ranked_pythonic():
ranked = [100, 90, 90, 80]
player = [70, 80, 105]
result = solve_ranked_pythonic(ranked, player)
assert result == [4, 3, 1]
def test_ranked_efficient():
ranked = [100, 90, 90, 80]
player = [70, 80, 105]
result = solve_ranked_efficient(ranked, player)
assert result == [4, 3, 1]