Sunglasses¶
A brand of sunglasses identifies their models using two codes
(strings). The first code, size code, represents the size and
consists of two digits, indicating the diameter of the glass, followed
by a hyphen and two digits more, indicating the length of the
bridge. For example, the code '52-14' represents a model with a
diameter of 52 and a bridge length of 14. The second code,
identification code, indicates the model name, for example,
'RB2014'.
The brand stores the information of the different models available in
a dictionary, where the key is a size code and the value is a list of
identification codes that are available for that size. For example,
the pair '52 -14': ['RB2014', 'RB2514', 'RJ2014'] indicates that
for the size code '52-14' there are the following models
'RB2014', 'RB2514' and 'RJ2014'.
In the module sunglasses (file sunglasses.py), write
the function diameters() that given a dictionary as
described, return a new dictionary where each key is a diameter and
each value is the list of models with this diameter, sorted
alphabetically. Both the diameter and the models in the resulting
dictionary are strings. In this new dictionary there
must be as many keys as there are different diameters in the given
dictionary and the list associated with each key cannot contain
repeated models.
For example:
>>> du = {
... '52-14':['RB2014','RB2213','RJ2014'],
... '54-14':['RJ2014','RB2514'],
... '52-16':['RJ2514','RB2014'],
... '55-14':['RB2014']
... }
>>> dr = diameters(du)
>>> if dr != {
... '52':['RB2014','RB2213','RJ2014','RJ2514'],
... '54':['RB2514','RJ2014'],
... '55':['RB2014']
... }:
... print(dr)
More tests are provided in file test-diameters.txt.
Solution:
A solution is provided in file sunglasses.py.