AnalystPath

Rainwater Held by a Skyline

PandasHardSenior level~10 min

Problem

A city skyline is described by the DataFrame `skyline`, where each row gives a `position` (left-to-right index) and the `wall_m` value -- the metres of a unit-wide rooftop ledge at that position. After a storm, water pools on the rooftops: at any position the standing water level equals the shorter of the tallest ledge to its left (inclusive) and the tallest ledge to its right (inclusive); the water depth there is that level minus the position's own `wall_m`. Return a DataFrame with a single column `pooled_water` holding the total volume of water pooled across the whole skyline.

Input data

Example rows — the live problem includes the full dataset.

skyline
positionwall_m
10
21
30
42
51

Expected output

Your answer should return 1 row with the columns pooled_water.

Starter code (Pandas (Python))

import pandas as pd

def pooled_rainwater(skyline) -> pd.DataFrame:
    # Your code here
    return skyline

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions