Tournament rounds
- O(log n) · Easy
- Free
- Python
- JavaScript
- division
- loops
Problem
Marta has judged the fair's thumb wrestling tournament for twenty years, and her rule is simple. It is single elimination: each round, everyone still in plays in pairs and the winner of each pair moves on to the next. If a round has an odd number of players, one rests and moves on without playing.
Write a function that takes how many players there are (a whole number, 1 or more) and returns how many rounds it takes to have a single champion. With 5: the first round has two pairs and one rests, so 3 move on; the second has one pair and one rests, so 2 move on; the third is the final. That is 3 rounds. With 1 player there is already a champion: 0 rounds.
Examples
The example
5 → 3
Eight, no one rests
8 → 3
Two, straight to the final
2 → 1
Six, odd in the second round
6 → 3
Nine, one more than eight
9 → 4
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def rounds(players):
passJavaScript
function rounds(players) {
}It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.