Gifts budget (4 points)¶
A group of persons (family, friends, etc.) is going to analyse data concerning Christmas gifts.
They have a file in which each line corresponds to a gift and has 4
data fields: the name of the person that has bought the gift
(str), the name of the person that has received it
(str), the price of this gift in euros (int)
and the holiday in which this gift has been offered
(str). You can assume that no-one has given more than one
gift to the same person in the same Christmas event.
The last data field may have different names referring to the same
holiday but they will all contain a characterization
string. Moreover, these names may include uppercase and lowercase
letters. For example, the names 'Els tres reis', "Els Reis
d'Orient" and 'reis' refer to the same holiday and their
characterization string is 'reis'.
Two examples of such a file are files gifts1.txt and
gifts2.txt that you must download in your working folder. Next the 5 first lines of file gifts1.txt are shown:
Aina,Roger,34,cagatio
Joana,Aina,45,reis
Joana,Pol,40,Tio
Joana,Carles,32,Reis Orient
Carles,Joana,35,el tio
Besides this file, this group of persons will need a dictionary
(dict) with an item for each person that has bought a
gift. The key of this dictionary is the name of this person
(str) and the value is a list with two items; the first
one is the number of persons that have received a gift from this
person (int) and the second one is the total amount spent
in gifts by this person (int).
Save the two following functions to file gifts.py.
Write function
gifts1()that takes the name of a file as the one described (str) and a characterization string (str) and returns a dictionary (dict) as the one described, for those gifts given in the holiday corresponding to the characterization string (2.5 points). Examples:>>> db1 = gifts1('gifts1.txt', 'reis') >>> if db1 != {'Joana': [2, 77], 'Carles': [2, 69], 'Roger': [2, 76], 'Pol': [2, 92], 'Aniol': [1, 44], 'Sira': [1, 38]}: ... print(db1) >>> db2 = gifts1('gifts1.txt', 'tio') >>> if db2 != {'Aina': [1, 34], 'Joana': [1, 40], 'Carles': [2, 76], 'Roger': [2, 74], 'Pol': [3, 148], 'Aniol': [1, 38], 'Sira': [1, 39]}: ... print(db2)
Note
You have more tests in file
test-gifts1.txtWrite function
gifts2()that takes a dictionary (dict) as the one described and a characterization string (str) and creates a file. The name of this file begins by the letter'f', continues with the characterization string and has the extension'txt'. Example: for the characterization string'reis', the name of the file is'freis.txt'. Each line of this file corresponds to an item of the dictionary and has three data fields separated by a hyphen: the name of the person and the two integers in the list, in the same order. Lines in this file may be in any order (1.5 points). Examples:>>> db1 = {'Joana': [2, 77], 'Carles': [2, 69], 'Roger': [2, 76], 'Pol': [2, 92], 'Aniol': [1, 44], 'Sira': [1, 38]} >>> gifts2(db1, 'reis') >>> with open('freis.txt', 'r') as f: ... s1= f.readlines() >>> s1.sort() >>> s1 ['Aniol-1-44\n', 'Carles-2-69\n', 'Joana-2-77\n', 'Pol-2-92\n', 'Roger-2-76\n', 'Sira-1-38\n']
Note
You have more tests in file
test-gifts2.txt