-
Notifications
You must be signed in to change notification settings - Fork 8
/
tournament_simulation.py
50 lines (34 loc) · 1.92 KB
/
tournament_simulation.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
"""
Task 1 of the Moringa Codility test
The task is to simulate a tournament
The tournament is a knockout tournament
The tournament is represented as a list of integers
The list of integers represents the skills of the players
The skills of the players are represented as integers
The skills of the players are unique
The skills of the players are distinct
The skills of the players are positive
The skills of the players are less than or equal to 10^9
"""
def solution(skills):
#Lets try to sort the players based on skills
players_sort= sorted(enumerate(skills), key=lambda x: x[1], reverse=True)
#How about i try creating a dictonary that will keep each player and their skills
information_of_the_player= {player[0]: {"skills": player[1], "round" : 0} for player in players_sort}
#We can try to simulate the players ground in the tourbament
#This will calll for a while loop bby us checking the length
while len(information_of_the_player) > 1:
newwer_player = {}
for i in range(0, len(players_sort), 2):
player_one, player_two = players_sort[i], players_sort[i+1]
#Getting to see the winner loser side of the game
winner, loser = max(player_one, player_two, key=lambda x: x[1])
#We can update the rounds of elimination
information_of_the_player[loser[0]]["round"] += 1
newwer_player[winner[0]] = information_of_the_player[winner[0]]
#We can update the players_sort
players_sort = sorted(newwer_player.items(), key=lambda x: x[1]["skills"], reverse=True)
information_of_the_player = newwer_player
#We need to convert the result to a list
result = [information_of_the_player[player][1]["round"] for player in range(len(skills))]
return result