Drill

ProblemsC# › games

Look up a player level

mediumgamesBinary searchArraysC#

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

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

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int LevelProgress(int xp, List<int> thresholds) {
    
}

Worked examples

CallResult
LevelProgress(50, new List<int> { 10, 30, 60, 100 })1
LevelProgress(100, new List<int> { 10, 30, 60, 100 })3
LevelProgress(5, new List<int> { 10, 30, 60 })-1
LevelProgress(0, new List<int> { })0

Hint

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

Reference solution in C#
public int LevelProgress(int xp, List<int> thresholds) {
    if (thresholds.Count == 0) return 0;
    if (xp < thresholds[0]) return -1;
    int result = 0;
    for (int i = 0; i < thresholds.Count; i++) {
        if (thresholds[i] <= xp) result = i;
    }
    return result;
}

The same problem in another language

More games problems in C#