-
Notifications
You must be signed in to change notification settings - Fork 8
/
tournament_simulation.js
63 lines (50 loc) · 2.22 KB
/
tournament_simulation.js
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
/****
*
* """
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
"""
*/
function solution(skills) {
// Sort the players based on skills
const playersSort = skills.map((skill, index) => ({ index, skill }))
.sort((a, b) => b.skill - a.skill);
// Create a dictionary to keep each player and their skills
const informationOfThePlayer = playersSort.reduce((acc, player) => {
acc[player.index] = { skills: player.skill, round: 0 };
return acc;
}, {});
// Simulate the players' rounds in the tournament
while (Object.keys(informationOfThePlayer).length > 1) {
const newwerPlayer = {};
for (let i = 0; i < playersSort.length; i += 2) {
const [playerOne, playerTwo] = [playersSort[i], playersSort[i + 1]];
// Determine the winner and loser of the game
const [winner, loser] = playerOne.skill > playerTwo.skill ? [playerOne, playerTwo] : [playerTwo, playerOne];
// Update the rounds of elimination
informationOfThePlayer[loser.index].round += 1;
newwerPlayer[winner.index] = informationOfThePlayer[winner.index];
}
// Update the playersSort
playersSort.splice(0);
Object.entries(newwerPlayer).forEach(([index, { skills }]) => {
playersSort.push({ index: parseInt(index), skill: skills });
});
playersSort.sort((a, b) => b.skill - a.skill);
// Update the informationOfThePlayer
Object.keys(newwerPlayer).forEach(index => {
informationOfThePlayer[index] = newwerPlayer[index];
});
}
// Convert the result to a list
const result = Object.keys(informationOfThePlayer).map(index => informationOfThePlayer[index].round);
return result;
}