Drill

ProblemsPython › games

Look up a player level

mediumgamesPython

A game tracks experience points against a table of ascending thresholds. Find the highest level reached.

level_progress(xp: int, thresholds: list<int>) → int

Solve it in the editor →

Where you start

def level_progress(xp: int, thresholds: list[int]) -> int:
    

Worked examples

CallResult
level_progress(50, [10, 30, 60, 100])1
level_progress(100, [10, 30, 60, 100])3
level_progress(5, [10, 30, 60])-1
level_progress(0, [])0

Hint

Walk the thresholds and remember the last one that was met.

Reference solution in Python
def level_progress(xp: int, thresholds: list[int]) -> int:
    if len(thresholds) == 0:
        return 0
    if xp < thresholds[0]:
        return -1
    result = 0
    for i in range(len(thresholds)):
        if thresholds[i] <= xp:
            result = i
    return result

The same problem in another language

More games problems in Python