Reservoir Levels

Source: Dec 2024, Task 4 (DTU 02002/02003). Study solution — not an official answer key.

Problem

Given hourly water levels, report indices where the level drops strictly more than 150 compared to the previous measurement.

Write reservoir_levels(levels) returning a list of such indices.

Example
>>> reservoir_levels([1320, 1307, 1295, 1102, 1360, 1395, 1101, 1208])
[3, 6]

Approach

For each index i >= 1, if levels[i-1] - levels[i] > 150, include i.

Solution

Show solution Hide solution
Solution
def reservoir_levels(levels):
    return [
        i
        for i in range(1, len(levels))
        if levels[i - 1] - levels[i] > 150
    ]