Bus4

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 bus4 (file bus4.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:

Move_Row_psgD(psgD, dest, delta)

such that

given

  • psgD a dict as described above

  • dest a str with a stop

  • delta an int the displacement

updates psgD by changing all the rows for passengers with the dest destination and moving them delta rows in the bus. This displacement can be positive or negative but moving is always calculated by adding it to the current row.

For example, given the passenger dict above:


>>> res = Move_Row_psgD(d, 'Amer', 2)

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

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

Doctests are available in the Move_Row_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 in the bus at each pickup point. To that purpose, you are asked to deliver the following function:

Construct_pickupD(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 in the bus.

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

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 in the bus.

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

    • the destination (str)

    • the passenger name (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 = {
... 'Bcn-T': [('Olot', 'Roser Tulsà', 2, 1), ('Amer', 'Lluís Vila', 2, 3), ('Amer', 'Marc Penosa', 1, 1)],
... 'Bcn-G': [('Olot', 'Rosa Badosa', 1, 4), ('Olot', 'Pau López', 2, 4), ('Amer', 'Pep Espígol', 1, 2)],
... }

Doctests are available in the Construct_pickupD.test file.


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

Sort_pickupD(pickupD)

such that

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

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

  1. The destination, in alphabetic order.

  2. The row number, in descending order.

  3. The passenger name, in alphabetic order.

For example:


>>> d =  { 
... 'Bcn-T': 
... [('Olot', 'Roser Tulsà', 2, 1), 
... ('Amer', 'Lluís Vila', 2, 3), 
... ('Amer', 'Ada Vila', 2, 4), 
... ('Amer', 'Marc Penosa', 1, 1)],
... 'Bcn-G': 
... [('Olot', 'Rosa Badosa', 1, 4), 
... ('Olot', 'Pau Ros', 2, 4),
... ('Olot', 'Angel Ros', 2, 3),
... ('Amer', 'Pep Espígol', 1, 2)]
... }


>>> dcorr = {
... 'Bcn-T': [
... ('Amer', 'Ada Vila', 2, 4), 
... ('Amer', 'Lluís Vila', 2, 3), 
... ('Amer', 'Marc Penosa', 1, 1), 
... ('Olot', 'Roser Tulsà', 2, 1)], 
... 'Bcn-G': [
... ('Amer', 'Pep Espígol', 1, 2),
... ('Olot', 'Angel Ros', 2, 3),
... ('Olot', 'Pau Ros', 2, 4), 
... ('Olot', 'Rosa Badosa', 1, 4)]
... }

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

Doctests are available in the Sort_pickupD.test file.