Capturing Rain Water

Link to exercise Capturing Rain Water

Let’s start by finding the maximum value at a lesser index for each value in the list.
Iterate from index 1 to the second to last index of histogram .
Within the loop, declare left_values and set it to every value up to but not including the current index.

This is confusing to me. Does it want to sum the values of the list up to that point, or does it want to create a new list an append it to this variable?

1 Like

1." Iterate from index 1 to the second to last index of histogram .":

this is asking your to iterate indices (for example using a loop).

  1. “Within the loop, declare left_values and set it to every value up to but not including the current index”:

this is asking you to: inside the loop from #1, do x (its instructions)

You should consider the type of the variable left_values, since it’s not going to be a single integer, is it going to be a list, dictionary, tuple, set, or something else?

I take it to mean,

left_values = histogram[:i]

but need to carry out more steps to confirm this.

1 Like

Your asking me to ask myself the very question I am asking, which is what data type it is. I assume its a list, an only a list, but the question in my opinion was ambiguous.

from time import time

#We’ll start with an input where we can easily determine the answer. Add the following variable to script.py:
small = [1, 0, 1]

#Now let’s use a more complicated input:
medium = [4, 2, 1, 3, 0, 1, 2]

#This input will collect no water, but it’s good to test these cases where something unusual might happen.
edge_case = [0, 2, 0]

#Define the rain_water() function, which has one parameter: histogram, a Python list of positive integers.
#Let’s start by finding the maximum value at a lesser index for each value in the list.
#Iterate from index 1 to the second to last index of histogram.
#Within the loop, declare left_values and set it to every value up to but not including the current index.
def rain_water(histogram):
	for i in range(1, len(histogram) - 1):
		left_values = []
####### TEST INPUTS HERE


####### NAIVE SOLUTION HERE


####### OPTIMIZED SOLUTION HERE


####### BENCHMARKING HERE

Is what I had, so based on what you said I am going to assume that I was close and so far (without going further) what you have makes sense in what it is asking. I just believe this question was too ambiguous.

that’s saying what to do with those values

is saying part of how to do it. also, that will be O(N^2) but should be O(N) because you only need to consider the previous max and the new value that is added

1 Like

It’s ambiguous in the sense it doesn’t tell you. Pedagogically the idea is to make one reflect on the characteristics of the types of groupings one knows.

It’s important not to skip the interrogative step because the efficiency in our code is by understanding the characteristics of the types. If you doubt which type, it’s better to do a search for when to use a list over a dictionary. If any doubt remains, then that’s also a great place to ask further.

If the exercise simply told you to use a list, when you have a similar situation come up you might not be as quick at recognizing which type is effective in the case.

The best exercise have questions that give enough room for critical thinking. This is because Python and coding is not a static concept, it is constantly changing, and sometimes methods will change too. If our manner in tackling these changes is rooted in good investigative technique, then it is easy enough to adjust.

I mean ambiguous as in it wasn’t clear, and since the theme of this course seems to ask questions among other things, that is what I am doing. It’s obvious its not asking for other data types. What wasn’t obvious is that in its wording if it wanted an integer or list.

#8 Add two new variables to the loop: right_values and right_max .
Use the same logic as before, but for values at indices greater than the current index.

I’m assuming this is what it is asking?

		right_values = histogram[i:]
		right_max = max(right_values)
from time import time

#We’ll start with an input where we can easily determine the answer. Add the following variable to script.py:
small = [1, 0, 1]

#Now let’s use a more complicated input:
medium = [4, 2, 1, 3, 0, 1, 2]

#This input will collect no water, but it’s good to test these cases where something unusual might happen.
edge_case = [0, 2, 0]
edge_case1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#Define the rain_water() function, which has one parameter: histogram, a Python list of positive integers.
#Let’s start by finding the maximum value at a lesser index for each value in the list.
#Iterate from index 1 to the second to last index of histogram.
#Within the loop, declare left_values and set it to every value up to but not including the current index.
#Below left_values, declare another variable: left_max. This should be the maximum value within left_values.
#Add print() statements to check out the variables.
#Add two new variables to the loop: right_values and right_max.
#Use the same logic as before, but for values at indices greater than the current index.
def rain_water(histogram):
	for i in range(1, len(histogram) - 1):
		left_values = histogram[:i]
		left_max = max(left_values)
		right_values = histogram[i:]
		right_max = max(right_values)
		print(right_values)
####### TEST INPUTS HERE


####### NAIVE SOLUTION HERE


####### OPTIMIZED SOLUTION HERE


####### BENCHMARKING HERE
rain_water(edge_case1)

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[6, 7, 8, 9]
[7, 8, 9]
[8, 9]

you’re looking for the tallest columns to your left and right respectively, because those are what will determine the water level at this location

the values to the left tell you the max to the left
the values to the right tell you the max to the right

the max to the right and the max to the left tells you the current water level

the current water level and the location of the bottom tells you the amount of water

1 Like

I just needed a simple confirmation, if not, point in the right direction. All that did was just frustrate me more than I am already with this course, which is unusual since all but this course and SQL have been excellent and well done unlike this course.