Balls

Let’s consider the following two dictionaries:

  1. A dictionary, we shall call id_city_D, where the key is an int with a person’s ID number and the value is a str with the name of her/his city of residence. For example:

    
    >>> id_city_D = {
    ... 77000767: 'Mataró', 88999000: 'Mataró', 99666333: 'Girona',
    ... 55444333: 'Bcn', 66777888: 'Vic', 
    ... 10111222: 'Bcn', 22111333: 'Girona',  33121212: 'Bcn',
    ... }
    
    
  2. A dictionary, we shall call id_balls_D, where the key is an int with a person’s ID number and the associated value is a list of str with the name of the swing and blues dances that person likes to dance. For example:

    
    >>> id_balls_D = { 
    ... 88999000: ['Lindy Hop'],
    ... 55444333: ['Rock', 'Lindy Hop'],
    ... 10111222: ['Lindy Hop', 'Blues'],
    ... 44555666: ['Lindy Hop', 'Blues', 'Rock'],
    ... 77000767: ['Lindy Hop', 'Shag'],
    ... }
    
    

In this context, you are required to deliver the following functions in the module balls (file balls.py):


The first function is:

ballsD_inv(id_city_D, id_balls_D)

such that

given id_city_D, id_balls_D, dict as described above.

returns the inverted dict of id_balls_D but only with the persons included in id_city_D, namely a dict where keys are the dance names (str) in id_balls_D and the associated value to each dance is a list of the dancers that like that dance and have a key in id_city_D.

For example:

>>> id_city_D = {
... 77000767: 'Mataró', 88999000: 'Mataró', 99666333: 'Girona',
... 55444333: 'Bcn', 66777888: 'Vic', 
... 10111222: 'Bcn', 22111333: 'Girona',  33121212: 'Bcn',
... }

>>> id_balls_D = { 
... 88999000: ['Lindy Hop'],
... 55444333: ['Rock', 'Lindy Hop'],
... 10111222: ['Lindy Hop', 'Blues', 'Rock'],
... 44555666: ['Lindy Hop', 'Blues', 'Rock'],
... 77000767: ['Lindy Hop', 'Shag'],
... }

>>> solD = ballsD_inv(id_city_D, id_balls_D)

>>> correctD = {
... 'Lindy Hop': [88999000, 55444333, 10111222, 77000767],
... 'Rock': [55444333, 10111222],
... 'Blues': [10111222],
... 'Shag': [77000767]}

Observe that the id 44555666 is not included anywhere in the resulting dict’s values because it is not a key in id_city_d.

Doctests are available in the ballsD_inv.test file.


The second function is:

ballsD_gen(id_city_D, id_balls_D)

such that

given the dict id_city_D and id_balls_D as described at the beginning

returns a dict where the key is a str with a city name included in id_city_D and the associated value is a list of two-component tuple, one for each dance and dancer from that city according to id_balls_D. Specificly, each tuple has two values: an int with the dancer Id number and a str which is the name of the dance. The dancers that do not appear in id_city_D must be listed under the 'X' key.

Notice that the cities where there is no dancer are not included in the dictionary’s keys.

For instance, in the following example the Id 44555666 is not a key in id_city_D, therefore it is listed under the 'X' and the associated value is a list which includes three tuples with that ID number, one for each dance s/he likes.

For example:


>>> id_city_D = {
... 77000767: 'Mataró', 88999000: 'Mataró', 99666333: 'Girona',
... 55444333: 'Bcn', 66777888: 'Vic', 
... 10111222: 'Bcn', 22111333: 'Girona',  33121212: 'Bcn',
... }

--fi11
--ini12

>>> id_balls_D = { 
... 88999000: ['Lindy Hop'],
... 55444333: ['Rock', 'Lindy Hop'],
... 10111222: ['Lindy Hop', 'Blues'],
... 44555666: ['Lindy Hop', 'Blues', 'Rock'],
... 77000767: ['Lindy Hop', 'Shag'],
... }

--fi12
--ini2

>>> solD = ballsD_gen(id_city_D, id_balls_D)

>>> correctD = {
... 'Mataró': [(88999000, 'Lindy Hop'), (77000767, 'Lindy Hop'), (77000767, 'Shag')],
... 'Bcn': [(55444333, 'Rock'), (55444333, 'Lindy Hop'),  (10111222, 'Lindy Hop'), (10111222, 'Blues')],
... 'X': [(44555666, 'Lindy Hop'), (44555666, 'Blues'), (44555666, 'Rock')],
... }

Doctests are available in the ballsD_gen.test file.


The third function is:

ballsD_sort(balls_D)

such that

given a dict as the one produced by the previous function

modifies it by ordering the lists for each entry according to the following criteria:

  1. Alphabetically by the name of the dance.

  2. Increasingly by the Id number.

For example:


>>> balls_D = {
... 'Mataró': [(88999000, 'Lindy Hop'), (77000767, 'Lindy Hop'), (77000767, 'Shag')],
... 'Bcn': [(55444333, 'Rock'), (55444333, 'Lindy Hop'), (10111222, 'Lindy Hop'), (10111222, 'Blues')],
... 'X': [(44555666, 'Lindy Hop'), (44555666, 'Blues'), (44555666, 'Rock')],
... }
>>> ballsD_sort(balls_D)

>>> correctD = {
... 'Mataró': [(77000767, 'Lindy Hop'), (88999000, 'Lindy Hop'), (77000767, 'Shag')],
... 'Bcn': [(10111222, 'Blues'), (10111222, 'Lindy Hop'), (55444333, 'Lindy Hop'), (55444333, 'Rock')],
... 'X': [(44555666, 'Blues'), (44555666, 'Lindy Hop'), (44555666, 'Rock')]}

Doctests are available in the ballsD_sort.test file.