Discounts (3 points)¶
A department store has applied a Christmas campaign 2x1 meaning that if a customer buys 2 products, they pay only the most expensive one. The department store wants to analyse the amount not bought, i. e., the amount corresponding to the products that have been given for free during this campaign.
The department has saved in a nested list all the two
products purchases that have taken profit from this campaign. Each
sublist contains the names of the two products bought by a customer
(str). Product prices are stored in a dictionary
(dict), in which the key is the name of a product
(str) and the value its price (int).
Write function discounts() that takes a list and a dictionary
as described, and returns another dictionary for those products that
have been given for free in this campaign. The key of this dictionary
is the product name (str) and the value is a list of
two elements: the first one is the number of units of this product
that have been given for free (int) and the second element is
the total amount saved by the customers for this product
(int), i.e., the number of free units multiplied by the
price. When two products have the same price, we must consider the
first one in the sublist.
Hint: You can compute the output dictionary directly. Alternatively, you can compute first an auxiliary dictionary in which the key is the product name and the value is the number of units of this product that have been given for free, and then use this auxiliary dictionary to compute the requested one.
Save this function in file discounts.py.
Examples:
>>> lp1 = [['shirt', 'scarf'], ['scarf', 'coat'], ['coat', 'shirt'],
... ['shirt', 'blouse'], ['skirt', 'coat'], ['scarf', 'skirt']]
>>> dprice1 = {'shirt': 45, 'blouse': 45, 'skirt': 55, 'scarf': 25, 'coat': 90}
>>> doutput1 = discounts(lp1, dprice1)
>>> if doutput1 != {'scarf': [3, 75], 'shirt': [2, 90], 'skirt': [1, 55]}:
... print(doutput1)
Note
More tests are provided in file test-discounts.txt.