Count tournament rounds
A single-elimination tournament keeps halving the field until one champion remains. How many rounds does that take?
- Each round pairs up as many teams as possible; odd teams get a bye.
- The number of teams per round is ceil(teams / 2).
- One team or fewer needs zero rounds.
tournament_rounds(teams: int) → int
Where you start
def tournament_rounds(teams: int) -> int:
Worked examples
| Call | Result |
|---|---|
tournament_rounds(1) | 0 |
tournament_rounds(2) | 1 |
tournament_rounds(4) | 2 |
tournament_rounds(5) | 3 |
Hint
Repeatedly halve (rounding up) and count until one remains.
Reference solution in Python
def tournament_rounds(teams: int) -> int:
if teams <= 1:
return 0
rounds = 0
t = teams
while t > 1:
t = (t + 1) // 2
rounds += 1
return rounds