Perfumes¶
The company SMELLS S.A. produces several perfumes and saves the data
in a list of sublists where each sublist corresponds to a perfume and
has the following data: perfume name (string), perfume type (a string
that can be 'PARFUM', 'EAU_PARFUM', 'EAU_TOILETTE' or
'EAU_COLOGNE'), net content in ml (int) and price in euros
(float).
Save the following functions into the same file named perfumes.py.
- perfumes.perfumes1(lperfumes)¶
Given a perfumes list as described, this function returns a dictionary where the key is a net content and the value is a list with those perfume names that have this net content. Perfume names in these lists are in the same order as in the given list.
>>> l = [['Rochis1', 'EAU_TOILETTE',100,78],['Rochis2','EAU_TOILETTE',50,42], ... ['Challenge','EAU_PARFUM',100,78],['Gege','EAU_COLOGNE',200,43]] >>> d = perfumes1(l) >>> if d != {100:['Rochis1','Challenge'], 50:['Rochis2'], 200:['Gege']}: ... print(d)
Note
More tests are provided in file test-perfumes1.txt.
- perfumes.perfumes2(lperfumes)¶
Given a perfumes list as described, this function returns a dictionary where the key is a perfume type and the value is a list of sublists of perfumes of this type. Each sublist has two elements: the perfume name and its price. Perfume names in these lists are in the same order as in the given list.
>>> l = [['Rochis1', 'EAU_TOILETTE',100,78],['Rochis2','EAU_TOILETTE',50,42], ... ['Challenge','EAU_PARFUM',100,82],['Gege','EAU_COLOGNE',200,43]] >>> d = perfumes2(l) >>> if d != {'EAU_TOILETTE': [['Rochis1',78], ['Rochis2',42]], ... 'EAU_PARFUM': [['Challenge',82]], 'EAU_COLOGNE': [['Gege',43]]}: ... print(d)
Note
More tests are provided in file test-perfumes2.txt.
- perfumes.buy1(perfumes, euros)¶
Given a dictionary as the one returned by function
creaperfumes2, a perfume type and an amount of euros, this function returnsTrueif there is a perfume in the given dictionary of the given type that can be bought with the given amount andFalseotherwise.>>> d = {'EAU_TOILETTE': [['Rochis1',78], ['Rochis2',42]], ... 'EAU_PARFUM': [['Challenge',82]], 'EAU_COLOGNE': [['Gege',43]]} >>> buy1(d, 'EAU_TOILETTE', 50) True >>> buy1(d, 'EAU_PARFUM', 70) False
Note
More tests are provided in file
test-buy1.txt.
- perfumes.buy2()¶
Given a dictionary as the one returned by function
creaperfumes2and an amount of euros, this function returns a list with those perfume names that have a price less than or equal to the given amount. This list must be ordered lexicographically.>>> d = {'EAU_TOILETTE': [['Rochis1',78], ['Rochis2',42]], ... 'EAU_PARFUM': [['Challenge',82]], 'EAU_COLOGNE': [['Gege',43]]} >>> buy2(d, 50) ['Gege', 'Rochis2'] >>> buy2(d,25) []
Note
More tests are provided in file
test-buy2.txt.
Solution
A solution of these functions is provided in file perfumes.py.