Bus¶
Consider a dictionary (dict) with information about the passengers of an interurban bus.
Let’s call it a passengers dictionary (psgD).
The keys in psgD are person identifiers, each composed of a 2-component tuple with a number (int) and one capital letter (str).
The values in psgD are 4-component tuple with:
the passenger’s name (
srt)the row and seat where his/her is supposed to seat (
int)the passenger’s destination or stop (
srt)
For example:
>>> d = { ... (771, 'L'): ('Vila', 1, 3, 'Amer'), ... (122, 'D'): ('Badosa', 1, 4, 'Olot'), ... (563, 'P'): ('Lopez', 2, 4, 'Olot'), ... (444, 'N'): ('Penosa', 1, 1, 'Amer'), ... (765, 'P'): ('Espigol', 1, 2, 'Amer'), ... }
You are required to implement the following functions and deliver all of them in the same module bus (file bus.py). The tentative weight for each problem is 60%, 10%, 20% and 10% respectively, although the weight of the former function could be increased.
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.
First we need to generate a new dictionary for the bus driver indicating the passengers that are suposed to get off the bus at each stop. To that purpose, you are asked to deliver the following function:
- gen_stops_D(psgD)¶
such that
For example:
>>> d = { ... (771, 'L'): ('Vila', 1, 3, 'Amer'), ... (122, 'D'): ('Badosa', 1, 4, 'Olot'), ... (563, 'P'): ('Lopez', 2, 4, 'Olot'), ... (444, 'N'): ('Penosa', 1, 1, 'Amer'), ... (765, 'P'): ('Espigol', 1, 2, 'Amer'), ... } --in-fi >>> dcorr = { ... 'Amer' : ['Vila', 'Penosa', 'Espigol'], ... 'Olot': ['Badosa', 'Lopez'], ... }
Doctests are available in the gen_stops_D.test file.
Second, we want the lists in all values of the dict produced by the previous function to be aphabetically ordered:
- sort_stops_D(stpD)¶
such that
given stpD a
dictas described for the output of the previous functionupdates stpD by alphabetically ordering the names in the lists in its values and nothing else.
For example:
>>> d = { ... 'Amer' : ['Vila', 'Penosa', 'Espigol'], ... 'Olot': ['Lopez', 'Badosa'], ... } >>> sort_stops_D(d) >>> dcorr = { ... 'Amer' : ['Espigol', 'Penosa', 'Vila'], ... 'Olot': ['Badosa', 'Lopez'], ... }
Doctests are available in the sort_stops_D.test file.
Third, we need a function to support the change of the destination of a particular passenger given the passenger’s name:
- upd_dest_D(psgD, name, newdest)¶
such that
given psgD a
dictas described aboveupdates psgD by changing the destination of the passenger named name. If there is no passenger with that name in psgD then it is not modified.
returns the passenger identifier (
tuple) and his/her new destination (str). If there is no passenger with that name in psgD then the empty tuple is returned as the passenger’s identifier.
For example:
>>> d = { ... (771, 'L'): ('Vila', 1, 3, 'Amer'), ... (122, 'D'): ('Badosa', 1, 4, 'Olot'), ... (563, 'P'): ('Lopez', 2, 4, 'Olot'), ... (444, 'N'): ('Penosa', 1, 1, 'Amer'), ... (765, 'P'): ('Espigol', 1, 2, 'Amer'), ... } >>> res = upd_dest_D(d, 'Vila', 'Olot') >>> dref = { ... (122, 'D'): ('Badosa', 1, 4, 'Olot'), ... (563, 'P'): ('Lopez', 2, 4, 'Olot'), ... (444, 'N'): ('Penosa', 1, 1, 'Amer'), ... (765, 'P'): ('Espigol', 1, 2, 'Amer'), ... (771, 'L'): ('Vila', 1, 3, 'Olot'), ... } >>> if res == ((771, 'L'), 'Olot') and d == dref: ... True ... else: ... (res, d) True >>> res = upd_dest_D(d, 'Sanchez', 'Olot') >>> if res == ((), 'Olot') and d == dref: ... True ... else: ... (res, d) True
Doctests are available in the upd_dest_D.test file.
Fourth, we wish to generate and sort a list with the information from a given passengers dict as described above at the beginning above.
To that purpose you have to implement the following function:
- sort_pass_L(psgD)¶
such that
For example:
>>> d = { ... (322, 'K'): ('Tulsà', 2, 1, 'Olot'), ... (771, 'L'): ('Vila', 2, 3, 'Amer'), ... (122, 'D'): ('Badosa', 1, 4, 'Olot'), ... (563, 'P'): ('Lopez', 2, 4, 'Olot'), ... (444, 'N'): ('Penosa', 1, 1, 'Amer'), ... (765, 'P'): ('Espigol', 1, 2, 'Amer')} >>> lcorr = [ ... ('Vila', 2, 3, 'Amer'), ... ('Espigol', 1, 2, 'Amer'), ... ('Penosa', 1, 1, 'Amer'), ... ('Lopez', 2, 4, 'Olot'), ... ('Tulsà', 2, 1, 'Olot'), ... ('Badosa', 1, 4, 'Olot')] >>> sort_pass_L(d) == lcorr True
Doctests are available in the sort_pass_L.test file.