Skip to the content

Level by experience

Problem

In the Ash Towers everyone starts at level 1, and every level costs experience to clear, some more than others. How far do your xp points take you?

Write a function that takes costs, a list of positive integers (the price to go from level 1 to 2, then from 2 to 3, and so on), and xp, an integer from 0 up. Return the level you end on, as an integer.

Levels are paid in order: if you can afford one, you pay and go up; if not, you stay put, even if the next one is cheaper. Exactly enough still counts. Pay them all and you land past the last one: with three costs, on level 4. An empty list leaves you on level 1.

With costs = [100, 150, 200] and xp = 280: you pay 100 (180 left), pay 150 (30 left), and 200 is too much. You end on level 3.

Examples

  • The one from the example

    [100, 150, 200], 280 → 3

  • Exactly enough

    [40, 60, 90], 100 → 3

  • Enough for all of them

    [10, 20, 30], 500 → 4

  • Not even the first one

    [25, 5], 20 → 1

  • Exactly enough for all of them

    [30, 30], 60 → 3

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def level(costs, xp):
    pass

JavaScript

function level(costs, xp) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →