Bus3

Consider a dictionary (dict) with information about the passengers of an interurban bus. Let’s call it the passengers dictionary (psgD) where the keys are passenger identifiers, each composed of a 2-component tuple with following passenger’s info:

  • the id number (str)

  • the name (str) composed of two words separated by ' '.

The value associated to each passenger is a 4-component list with information about the passenger’s trip:

  • the town of origin (srt)

  • the town of destination or stop (srt)

  • the row and seat assigned to that passenger (int)

For example:


>>> d = {
... ('322K', 'Roser Tulsà'): ['Bcn-T', 'Olot', 2, 1],
... ('771L', 'Lluís Vila'): ['Bcn-T', 'Amer', 2, 3],
... ('122D', 'Rosa Badosa'): ['Bcn-G', 'Olot', 1, 4],
... ('563P', 'Pau López'): ['Bcn-G', 'Olot', 2, 4],
... ('444N', 'Marc Penosa'): ['Bcn-T', 'Amer', 1, 1],
... ('765P', 'Pep Espígol'): ['Bcn-G', 'Amer', 1, 2]
... }

You are required to implement the following functions and deliver all of them in the same module bus3 (file bus3.py). The tentative weight for each problem is 25%, 50% and 25% respectively.

Warning

Do not leave pieces of code that are wither incomplete or contain syntax errors in the file to deliver since that would result in a 0.0 grade for all the functions.


The first function is:

Change_Dest_psgD(psgD, old, new)

such that

given

  • psgD a dict as described above

  • old, new, two str with distinations

updates psgD by changing all the values for passengers with the old destination to the new destination.

For example, the expected output for the passenger dict above would be:


>>> res = Change_Dest_psgD(d, 'Amer', 'Olot')

>>> dcorr = {
... ('322K', 'Roser Tulsà'): ['Bcn-T', 'Olot', 2, 1], 
... ('771L', 'Lluís Vila'): ['Bcn-T', 'Olot', 2, 3],
... ('122D', 'Rosa Badosa'): ['Bcn-G', 'Olot', 1, 4],
... ('563P', 'Pau López'): ['Bcn-G', 'Olot', 2, 4], 
... ('444N', 'Marc Penosa'): ['Bcn-T', 'Olot', 1, 1], 
... ('765P', 'Pep Espígol'): ['Bcn-G', 'Olot', 1, 2]
... }

>>> d == dcorr and res == None
True

Doctests are available in the Change_Dest_psgD.test file.


The second function is intended to construct a new dictionary for the bus driver that lists the passengers that are suposed to get off the bus at each stop. To that purpose, you are asked to deliver the following function:

Construct_stopsD(psgD)

such that

given psgD a passenger’s dict as described above

returns a new dict where:

  1. The keys are the stops (str) where at least one passenger is supposed to get out of the bus.

  2. The value associated to each key is a list with the passengers with the passengers that are supposed to get out of the bus at that stop. Each passenger is represented by a 4-place tuple with:

    • the passenger last name (str)

    • the passenger id number (str)

    • the row and seat numbers (int)

The input dict should not be modified.

For example:


>>> d = {
... ('322K', 'Roser Tulsà'): ['Bcn-T', 'Olot', 2, 1],
... ('771L', 'Lluís Vila'): ['Bcn-T', 'Amer', 2, 3],
... ('122D', 'Rosa Badosa'): ['Bcn-G', 'Olot', 1, 4],
... ('563P', 'Pau López'): ['Bcn-G', 'Olot', 2, 4],
... ('444N', 'Marc Penosa'): ['Bcn-T', 'Amer', 1, 1],
... ('765P', 'Pep Espígol'): ['Bcn-G', 'Amer', 1, 2]
... }

--in-fi

--out-ini

>>> dcorr = {
... 'Olot': [('Tulsà', '322K', 2, 1), ('Badosa', '122D', 1, 4), ('López', '563P', 2, 4)], 
... 'Amer': [('Vila', '771L', 2, 3), ('Penosa', '444N', 1, 1), ('Espígol', '765P', 1, 2)]
... }

Doctests are available in the Construct_stopsD.test file.


The third function is intended to order all the lists in the values of the dict produced by the previous function:

Sort_stopsD(stopsD)

such that

given stopsD a dict as described for the output of the previous function

updates stopsD by ordering the passengers in the lists for each stop according to the following criteria (in this order of priority):

  1. The row, in ascending order.

  2. The last name, in alphabetic order.

  3. The id number in ascending order.

For example:


>>> d = {'Olot': [
... ('Tulsà', '322K', 2, 1),
... ('Badosa', '122D', 1, 4),
... ('Tulsà', '563P', 2, 4),
... ('Tulsà', '444R', 2, 2)],
... 'Amer': [
... ('Vila', '771L', 2, 3),
... ('Espígol', '765P', 1, 1),
... ('Espígol', '444N', 1, 2)
... ]}


>>> dcorr = {'Olot': [
... ('Badosa', '122D', 1, 4), 
... ('Tulsà', '322K', 2, 1), 
... ('Tulsà', '444R', 2, 2), 
... ('Tulsà', '563P', 2, 4)], 
... 'Amer': [
... ('Espígol', '444N', 1, 2), 
... ('Espígol', '765P', 1, 1), 
... ('Vila', '771L', 2, 3)]
... }

>>> res = Sort_stopsD(d)
>>> d == dcorr and res == None
True

Doctests are available in the Sort_stopsD.test file.