Python Challenge - Capturing Rainwater, Capturing Rainwater

This community-built FAQ covers the “Capturing Rainwater” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Capturing Rainwater

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def capturing_rainwater(heights): waterTotal = 0 prevHeights = [] if heights: maxHeight = heights[0] else: return 0 # Subtract preceding heights from current height (or max height if at a local maximum block to allow for local water levels) and remove negative and 0 values, the sum of this array results in the amount of water contained to the left by the current block for i in heights.copy(): subArr = [i-a for a in prevHeights] if i < maxHeight else [maxHeight-a for a in prevHeights] filterSub = [x for x in subArr if x > 0] print(i, filterSub, subArr, prevHeights) waterTotal += sum(filterSub) # Increase heights of blocks that have already been counted as having some water above them up to this water level to avoid double counting prevHeights = [i if x < i else x for x in prevHeights] prevHeights.append(i) # If at a local maximum block calculate the water for this local section (right of local maximum) if i > maxHeight: return waterTotal + capturing_rainwater(heights) # Remove from unprocessed heights to allow for recersion heights.pop(0) return waterTotal test_array = [4, 2, 1, 3, 0, 1, 2] # test_array = [2,1,3,1,2] # test_array = [3, 2, 1, 6, 5, 6, 2, 4] print(capturing_rainwater(test_array))
1 Like
def capturing_rainwater(heights): # Write your code here total = 0 for i in range(1,len(heights)-1): left = [] #print(" ",i) for it in reversed(range(i+1)): if max(heights[i],heights[it])!= heights[i]: left.append(max(heights[i],heights[it])) right = [] for it in range(i,len(heights)): if max(heights[i],heights[it])!=heights[i]: right.append(max(heights[i],heights[it])) if right == []: right = [-1] left = [-1] if left == []: left = [-1] right = [-1] right = max(right) left = max(left) #print(min(right,left)) #print(heights[i]) if right != -1 and left != -1: total += min(right,left)-heights[i] #print(total) #print() #print(left) #print(right) return total test_array = [4, 2, 1, 3, 0, 1, 2] print(capturing_rainwater(test_array))
1 Like
def capturing_rainwater(heights): return sum(max(0, min(max([0, *heights[i+1:]]), max([0, *heights[:i]])) - n) for i, n in enumerate(heights)) test_array = [4, 2, 1, 3, 0, 1, 2] print(capturing_rainwater(test_array))
1 Like

Most interesting challenge so far.

  • Check every row.
  • Remove the outer ‘lower values’.
  • Count the number of zeros.
  • Move one level up. Repeat this until you reach the upper value.
def capturing_rainwater(heights): if len(heights) == 0: # When heights is [] return 0 else: print(heights) # Remove outer lower values: if len(heights) > 2: while heights[0] <= heights[1] and len(heights) > 2: heights.pop(0) while heights[-1] <= heights[-2] and len(heights) > 2: heights.pop() new_array = [] for x in heights: new_array.append(x) zeros = 0 for i in range(max(heights)): # remove outer zeros: while new_array[0] == 0: new_array.pop(0) while new_array[-1] == 0: new_array.pop() zeros += new_array.count(0) # count zeros between 'walls' for j in range(len(new_array)): # voor elk item in regel: # each value minus 1 (as if you go one level up) if new_array[j] > 0: new_array[j] = new_array[j] - 1 return zeros test_array = [4, 2, 1, 3, 0, 1, 2] print(capturing_rainwater(test_array))

Anyone know how to learn what inputs are being tested when you click Test Code? I got 4/5 tests passed, so I suspect there’s some edge case I didn’t handle, but I’m not sure what it is. I did discover that one of the five tests has an empty list as an input.