Water Tanks¶
Let’s consider a water tank with a limited capacity which received a number of water contributions at different times. When the water accumulated exceeds the tank capacity there is tank overflow and the excess water is lost.
This information is represented in a list of three values:
the tank name
the tank capacity
a list quantities of water contributions
In this context, you are required to deliver the following functions in the module tanks (file tanks.py).
The first function is:
- fill_up(capacity, Lc)¶
such that
given capacity, a positive
int, andcLalistof positiveintreturns two values:
a
boolwhich isTrueis the tank got filled up (the water accumulated by the sequence of contributions in cL reached or exceeded the capacity, andFalseotherwise.an
intwhich is either the number of water contributions from cL needed to reach the tank’s capacity, or 0 if the tank did not get full.
For example:
>>> fill_up(20, [2, 3, 4, 1, 5, 1]) (False, 0) >>> fill_up(10, [1, 2, 1, 5, 10]) (True, 5) >>> fill_up(15, [2, 4, 8, 4, 2, 5, 7, 4]) (True, 4)
Doctests are available in the fill_up.test file.
The second function is:
- select_full(tL)¶
such that
For example:
>>> tankL = [ ... ['Carmel', 20, [2, 3, 4, 1, 5, 1]], ... ['Nou_Barris', 10, [1, 2, 1, 5, 10]], ... ['Sant_Pere_Martir', 15, [2, 4, 8, 4, 2, 5, 7, 4]], ... ] >>> select_full(tankL) [('Nou_Barris', 5), ('Sant_Pere_Martir', 4)]
Doctests are available in the select_full.test file.
Note
Implementing this function by calling the fill_up function is recommended to save time.
The third function is:
- update_full(tL)¶
such that
given tL a
listof lists where each sublist is the information of a water tank as described aboveupdates tL by modifying the list of contributions leaving exactly the ones that fill the tank capacity. Therefore, the contributions after the tank being full should be deleted. The last contribution that filled up the tank must be updated in order to exactly fill the tank capacity. The tanks that did not reach its full capacity will not be modified.
For example:
>>> tankL = [ ... ['Carmel', 20, [2, 3, 4, 1, 5, 1]], ... ['Nou_Barris', 10, [1, 2, 1, 5, 10]], ... ['Sant_Pere_Martir', 15, [2, 4, 8, 4, 2, 5, 7, 4]], ... ] >>> update_full(tankL) >>> tankL == [ ... ['Carmel', 20, [2, 3, 4, 1, 5, 1]], ... ['Nou_Barris', 10, [1, 2, 1, 5, 1]], ... ['Sant_Pere_Martir', 15, [2, 4, 8, 1]], ... ] True
Observe that the last contribution in 'Nou Barris' tank changed from 10 to 1 in order to have the sum of the list of contributions exactly the tank capacity of 10, as opposed to ‘Carel tank which did not change.
Doctests are available in the update_full.test file.
Note
Implementing this function by calling the fill_up function is recommended to save time.