Traveler expenses

The expenses of a traveler are represented in a list of sublists. In each sublist there are one-day expenses (float values). The traveler is visiting a single country and the expenses are in the currency of this country.

Save the following functions into file expenses.py.

  1. Write function dayexpenses() that takes a list corresponding to the expenses of one day, and modifies it by adding the total spent on that day as the last item of the list. Examples:

    >>> l= [2.0, 3.0, 4.0, 5.0]
    >>> dayexpenses(l)
    >>> l
    [2.0, 3.0, 4.0, 5.0, 14.0]
    >>> l = [3.2, 5.8, 11.4]
    >>> dayexpenses(l)
    >>> l
    [3.2, 5.8, 11.4, 20.4]
    

    Note

    More tests are provided in file dayexpenses.txt

  2. Write function expenses() that takes a list of sublists, as described in the first paragraph, and the conversion value of the currency of the visited country in euros, and modifies the given list of sublists in such a way that to each sublist the total spent on that day in euros is added as the last item of the sublist. In addition, this function must return the total spent in euros on that trip. This function must use the previous function dayexpenses().

    >>> ld= [[2.0, 3.0, 4.0, 5], [6.0, 7.0, 6.0, 8], [50.0, 10.0, 4.0, 5]]
    >>> total = expenses(ld, 0.11)
    >>> lres = [[2.0, 3.0, 4.0, 5, 1.54], [6.0, 7.0, 6.0, 8, 2.97], [50.0, 10.0, 4.0, 5, 7.59]]
    >>> total == 12.1 and lres == ld
    True
    
    

    Note

    More tests are provided in file expenses.txt

  3. Write function dayhighexpense() that takes a list corresponding to the expenses of one day and a certain amount (all in the same currency), and returns the value of the first specific expense that exceeds that amount. If no expense exceeds the given amount this function must return the value 0.

    >>> l= [22.0, 31.0, 14.0, 5.0]
    >>> dayhighexpense(l, 25.0)
    31.0
    >>> l = [31.2, 5.8, 36.4, 50.5, 33.6]
    >>> dayhighexpense(l, 35.0)
    36.4
    >>> dayhighexpense(l, 75.0)
    0
    

    Note

    More tests are provided in file dayhighexpense.txt

  4. Write function highexpense() that takes a list of sublists, as described in the first paragraph, and a given float value corresponding to a expense amount (all in the same currency), and returns the first specific expense that exceeds that amount. If no expense exceeds the given amount, the function must return the value 0. This function must use the previous function dayhighexpense().

    >>> ld= [[2.0, 3.0, 4.0, 5], [6.0, 7.0, 6.0, 8], [50.0, 10.0, 4.0, 5]]
    >>> highexpense(ld, 45)
    50.0
    >>> ld
    [[2.0, 3.0, 4.0, 5], [6.0, 7.0, 6.0, 8], [50.0, 10.0, 4.0, 5]]
    >>> ld = [[2.0, 77.0, 10.0], [6.0, 7.0, 6.0, 8.0], [50.0, 12.0]]
    >>> highexpense(ld, 45)
    77.0
    >>> ld
    [[2.0, 77.0, 10.0], [6.0, 7.0, 6.0, 8.0], [50.0, 12.0]]
    >>> highexpense(ld, 100)
    0
    >>> ld
    [[2.0, 77.0, 10.0], [6.0, 7.0, 6.0, 8.0], [50.0, 12.0]]
    
    

    Note

    More tests are provided in file highexpense.txt

Solution

A solution is provided in file expenses.py.