(you are here)

Drought

Let's solve Drought.

In this problem, we are asked to use rain forecasts to predict when a water tank will be filled. This walkthrough will briefly outline a way of keeping track of the tank's water levels, before discussing how break and continue statements give us greater control over loops.

Simulating the water tank (I)

The problem statement makes things pretty clear. Each day's forecast gives us the exact amount of water that falls into the tank. We should give the day the tank fills (and we're promised it will always fill) as our answer.

Much like the previous problem, in which we had to calculate the mean of several numbers, here we need a variable to keep a running total of the water in the tank. For example:

total_rainfall = 0
for day in range(1, N+1):
    # read an integer into current_rainfall
    total_rainfall += current_rainfall
total_rainfall = 0;
for (int day = 1; day <= N; day++) {
    // read an integer into current_rainfall
    total_rainfall += current_rainfall;
}

We've had a bit of experience reading and writing loops now, so it should be clear how the above code works. After each addition, total_rainfall will have taken the correct value for the corresponding day.

From here it is a simple matter to check if the tank is full:

total_rainfall = 0
for day in range(1, N+1):
    # read an integer into current_rainfall
    total_rainfall += current_rainfall
    if total_rainfall >= tank_capacity:
        # the tank is full
total_rainfall = 0;
for (int day = 1; day <= N; day++) {
    // read an integer into current_rainfall
    total_rainfall += current_rainfall;
    if (total_rainfall >= tank_capacity) {
        // the tank is full
    }
}

However, this is not exactly what the problem is asking for. We need to find out how many days it takes until the tank fills. (Equivalently, we need to find out on which day the tank is first full.) Using the if statement above, there are several ways of going about this. In my opinion, the best way is using a break statement.

About break and continue statements

(Note: in this section I specifically discuss break and continue statements as they are used in Python/C++. However, the descriptions I give here apply to most of the programming languages allowed, though they might call them something else. If you're unsure about your language, consult a textbook or teacher.)

Sometimes when we use loops, the code inside the loop starts to get a little long. There may be situations where something happens during loop execution and we don't want the rest of the inner code to run. It is usually possible to fix this with complicated if statements and extra variables. Fortunately, many languages provide cleaner ways of controlling loop flow.

Simulating the water tank (II)

Let's return to our rainfall simulation. Before, we noticed that we want to find out on which day the tank is first full. It follows that as soon as the tank is full, we can take the number of that day, print it, and end the simulation (that is, exit the loop).

This translates nicely into a break statement. Once the rain tank is full, we will instruct the computer to print out the number of days that have passed, before using a break statement to cleanly end the loop. Our code will look a little like this:

total_rainfall = 0
for day in range(1, N+1):
    # read an integer into current_rainfall
    total_rainfall += current_rainfall
    if total_rainfall >= tank_capacity:
        # the tank is full
        # print the value of day
        break;
total_rainfall = 0;
for (int day = 1; day <= N; day++) {
    // read an integer into current_rainfall
    total_rainfall += current_rainfall;
    if (total_rainfall >= tank_capacity) {
        //the tank is full
        // print the value of day
        break;
    }
}

Thus ends the simulation. There are other ways of ending the loop, but this is the simplest way I am aware of. Using an approach like this you should be able to score 100% for Drought.

Your turn!


Solve Drought to complete this tutorial.