id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

lake_volume


In this exercise, we ask: what is the volume of water that could fill all the lake beds in an island? For simplicity, the topography of the island will be described as a list of altitudes (above sea-level) of cross-sectional slices of the island, from west to east. Think of these as the heights of a bar graph. So the list [3, 1, 3] indicates an island that has cliffs (of height 3) on the west and east coasts, and a valley (of height 1) in the middle. The list [0, 2, 0] indicates an island where there are beaches (of height 0 above sea level) on the west and east coasts, and a mountain of height 2 in the middle. Your task is to compute the volume of water that could fill the valley(s) (making them lakes) if enough rain fell on the island. lake_volume([3, 1, 3]) would be 2, because the rain in the valley would accumulate from height 1 up to height 3 (at which point it would spill over the sides). SImilarly, lake_volume([3, 1, 4]) would still be 2, because any more and the water would overflow the west cliff. lake_volume([0, 2, 0]) would be 0, because all the water would run off. (That would still be true if it were [1, 2, 1].) And lake_volume([3, 1, 3, 0, 0, 2]) would be 6: one lake of volume 2 above the "1" in the west, and a bigger lake of volume 4 (2 + 2 above the two "0"s) to the east.


lake_volume([3, 1, 3]) → 2
lake_volume([3, 1, 2, 0, 9]) → 6
lake_volume([3, 1, 3, 0, 0, 4]) → 8

...Save, Compile, Run (ctrl-enter)

def lake_volume(heights):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 500 Post-solution available

Copyright Nick Parlante 2017 - privacy