Sorting Town Temperatures

Let’s consider a list with information regarding the weather temperatures measured in a number of towns. The information for each town is represented in a 4-component tuple with the following fields:

  • an identification number (int)

  • the town name (str)

  • a list of temperatures (list of float)

  • the updated flag (bool): True is the info is up to date and False otherwise.

For example:


>>> tL = [
... (1, 'Vilanova', [23.0, 20.0, 25.0, 28.0], True), 
... (2, 'Vilademuls', [24.0, 25.0, 21.0], False), 
... (3, 'Vilana', [16.5, 18.5], True), 
... (4, 'Vilamalla', [10.0, 20.0, 13.5, 28.0], False), 
... (5, 'Vilafranca', [27.5, 13.5, 28.0], True), 
... (6, 'Vila', [13.5, 27.5, 28.0], True)
... ]

An auxiliar function called print_townL is provided to you in the town_tools.py file. This function is not a code to be used in your implementation but rather a tool that can be useful when you are testing your results to display the data in a towns list.

def print_townL(tL):
    for m in tL:
        print(f'{m[0]:>4} {m[1]:<12}: {str(m[2]):<30} ;{m[3]}')

You are required to deliver the following functions in the module sort_town_temp (file sort_town_temp.py).


The function:

sort_townL(tL)

such that

given tL, a list as described above

returns a new list with exactly the same information but reordered according to the following criteria (in this order of priority):

  1. The ones that are not updated go before the ones that are.

  2. The name in alfabetic order.

For example:


>>> res = sort_townL(tL)

>>> corr = [
... (2, 'Vilademuls', [24.0, 25.0, 21.0], False), 
... (4, 'Vilamalla', [10.0, 20.0, 13.5, 28.0], False), 
... (6, 'Vila', [13.5, 27.5, 28.0], True), 
... (5, 'Vilafranca', [27.5, 13.5, 28.0], True), 
... (3, 'Vilana', [16.5, 18.5], True),
... (1, 'Vilanova', [23.0, 20.0, 25.0, 28.0], True)
... ]

Doctests are available in the sort_townL.test file.

Warning

The input list must remain intact.


The function:

sort_max_townL(tL)

such that

given tL, a list as described above

returns a new list with exactly the same information but reordered according to the following criteria (in this order of priority):

  1. The ones that are not updated go before the ones that are.

  2. The maximum temperature registered in the town, in descending order.

  3. The name in alfabetic order.

For example:


>>> res = sort_max_townL(tL)

>>> corr = [
... (4, 'Vilamalla', [10.0, 20.0, 13.5, 28.0], False), 
... (2, 'Vilademuls', [24.0, 25.0, 21.0], False), 
... (6, 'Vila', [13.5, 27.5, 28.0], True), 
... (5, 'Vilafranca', [27.5, 13.5, 28.0], True), 
... (1, 'Vilanova', [23.0, 20.0, 25.0, 28.0], True), 
... (3, 'Vilana', [16.5, 18.5], True),
... ]

Warning

This input list must not be modified by the function.

Doctests are available in the sort_max_townL.test file.


The function:

sort_tempL(tL)

such that

given tL, a list of town temperatures as described above

sorts all the lists of temperatures within tL in ascending order.

For example, see the effects of this function over the input list above:


>>> res = sort_tempL(tL)
>>> corr = [
... (1, 'Vilanova', [20.0, 23.0, 25.0, 28.0], True), 
... (2, 'Vilademuls', [21.0, 24.0, 25.0], False), 
... (3, 'Vilana', [16.5, 18.5], True), 
... (4, 'Vilamalla', [10.0, 13.5, 20.0, 28.0], False), 
... (5, 'Vilafranca', [13.5, 27.5, 28.0], True), 
... (6, 'Vila', [13.5, 27.5, 28.0], True)
... ]
>>> tL == corr and res == None
True

Warning

Notice that all temperatures in tL are now increasingly ordered but the rest of the information remain intact. In particular, all towns remain in their same position in the input list.

Doctests are available in the sort_tempL.test file.


The function:

sort_avg_townL(tL)

such that

given tL, a list as described above

returns a new list with exactly the same information but reordered according to the following criteria (in this order of priority):

  1. The average temperature in descending order.

  2. The name without the 'Vila' prefix in alphabetic order.

For example:


>>> res = sort_avg_townL(tL)

>>> corr = [
... (1, 'Vilanova', [23.0, 20.0, 25.0, 28.0], True), 
... (2, 'Vilademuls', [24.0, 25.0, 21.0], False), 
... (5, 'Vilafranca', [27.5, 13.5, 28.0], True), 
... (6, 'Vila', [13.5, 26.5, 28.0], True),
... (4, 'Vilamalla', [10.0, 20.0, 13.5, 28.0], False),
... (3, 'Vilana', [16.5, 18.5], True)
... ]

Warning

This input list must not be modified by the function.

Doctests are available in the sort_avg_townL.test file.