Bikes¶
Let’s consider a given dictionary 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 = {
... 66777888: 'Munchen', 55444333: 'Munchen',
... 88999000: 'Milano', 10111222: 'Milano', 33444555: 'Milano',
... 44555666: 'Barcelona', 22111333: 'Manchester'
... }
In this context, you are required to deliver the following functions in the module bikes (file bikes.py):
The first function is:
- bikeD_gen(id_city_D, id_bike_D)¶
such that
given
id_city_D, is a
dictas described aboveid_bike_D, is a
dictwhere the key is aintwith a person’s ID number and the corresponding value is astrwith the brand of his/her motor bike.returns a
dictwith the inverted info of id_bike_D but only with the persons included in id_city_D. Specifically the keys of the new dict are bike brands appearing as values in id_bike_D and the value corresponding to each key is alistof Id numbers, the ones that who appear in id_city_D and own a bike of that brand according to id_bike_D. The order of these lists is the same as in the input dictionary.
For example:
>>> id_bike_D = { ... 66777888: 'Ducati', 77888999: 'Ducati', 88999000: 'Ducati', ... 10111222:'KTM'} >>> solD = bikeD_gen(id_city_D, id_bike_D) >>> correctD = { ... 'Ducati': [66777888, 88999000], ... 'KTM': [10111222], ... } >>> if solD != correctD: ... print(solD)
Observe that the id 77888999 is not listed in 'Ducati’s value because it is not a key in id_city_d.
Doctests are available in the bikeD_gen.test file.
Now let’s consider the case where a person can own more that one bike.
Hence we have the dictionary id_bikes_D where the key is a int with a person’s ID number
and the associated value is a list of str with the brands of the motor bikes owned by that person.
For example:
>>> id_bikes_D = {
... 88999000: ['Ducati'],
... 77888999: ['Ducati', 'Triumph'],
... 33444555: ['Ducati'],
... 66777888: ['Ducati', 'BMW'],
... 44555666: ['KTM'],
... 10111222: ['Ducati','KTM'],
... }
The second function is:
- cityD_gen(id_city_D, id_bikes_D, brd)¶
such that
given
id_city_D, is a
dictas described at the beginningid_bikes_D, is a
dictas described abovebrd, a
strwith a motor bike brand namereturns a
dictwith all Idn*s in *id_city_D that own a motorbike whose brand is brd. The key is astrwith a city of residence according to id_city_D and the associated value is an aphabetically orderedlistof those person’s Idn (int). The owners of a bike of brand brd in id_bikes_D that do not appear in id_city_D must be listed under the'Unknown'key.Notice that the cities where there is no bike owner of this brand are not included in the dictionary’s keys.
For instance, observe the following example: in the first call, the Id
77888999is listed under the'Unknown'since it belongs to somebody who owns a Ducati but is not included in id_city_D. Furthermore, in the second call, all Ducati owners are listed under the'Unknown'key since the id_city_D provided is empty.
For example:
>>> solD = cityD_gen(id_city_D, id_bikes_D, 'Ducati') >>> correctD = { ... 'Milano': [10111222, 33444555, 88999000], ... 'Munchen': [66777888], ... 'Unknown': [77888999], ... } >>> if solD != correctD: ... print(solD) >>> solD = cityD_gen({}, id_bikes_D, 'Ducati') >>> correctD = {'Unknown': [10111222, 33444555, 66777888, 77888999, 88999000]} >>> if solD != correctD: ... print(solD) >>> solD = cityD_gen(id_city_D, id_bikes_D, 'KTM') >>> correctD = {'Milano':[10111222], 'Barcelona': [44555666]} >>> if solD != correctD: ... print(solD) >>> cityD_gen(id_city_D, {}, 'Triumph') {}
Observe that the second example, all 'Ducati owners are listed in the Unknown key since the is the is empty.
Doctests are available in the cityD_gen.test file.