Drill

ProblemsPython › games

Count tournament rounds

easygamesPython

A single-elimination tournament keeps halving the field until one champion remains. How many rounds does that take?

tournament_rounds(teams: int) → int

Solve it in the editor →

Where you start

def tournament_rounds(teams: int) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More games problems in Python