Fair stands¶
Every year, a city council holds a summer solstice fair, with several
stands. The city council has a file where each line contains the name
of the stand (str), its length (float) and the
product it offers (str), separated by a semicolon
(';'). In addition, there is a dictionary (dict) where
the key is the name of a product and the value is its type (both
str). There are only two types of products: food and
handicrafts. In the file, the stands are sorted in desdending order
of preference to participate in the fair (which depends on several
factors). Next, there is an example of such a file:
joan pera;2.2;cheese marta perez;2.3;leather georgina pi;1.8;jewels joana morales;1.9;sausages pere girbau;2.1;cakes maria marti;2.3;dresses jordi pons;2;jewels narcis punti;1.9;leather
You can download this example file:
stands.txt
This year, the city council wants to put food stands on one side of the main street and handicraft stands on the other side. Each side of the main street has a length that cannot be exceeded. The city council asks us to create two files: one with the names of the selected food stands and the other one with the names of the selected handicraft stands. The stands are assigned in the same order of appearance in the given file and are taken until they fill the corresponding side of the main street. If there is a stand that does not fit, but there is another of lesser preference that fits, this last stand must be assigned.
Save all functions into the same file named fair.py.
Write function
decode()that takes a line of the given file (str) and the given dictionary, and returns a (tuple) with the name (str), lenght (float) and product type (str) of the stand. Examples:>>> dic = {'cheese': 'food', 'leather': 'handicraft', ... 'sausages': 'food', 'cakes': 'food', ... 'dresses': 'handicraft', 'jewels': 'handicraft'} >>> decode('joan pera;2.2;cheese\n', dic) ('joan pera', 2.2, 'food') >>> decode('narcis punti;1.9;leather\n', dic) ('narcis punti', 1.9, 'handicraft') >>> decode('georgina pi;1.8;jewels\n', dic) ('georgina pi', 1.8, 'handicraft')
Note
More tests are provided in file
test-decode.txt.Write function
stands()that takes the names (str) of three files (fstands, ffood and fcrafts), a dictionary as the one described and the lengths (float) of the sides of the main street (lfood, lcraft). File with name fstands is a file as that described. File with name ffood must contain the names of food type stands, a name at each line, and file with name fcrafts must contain the names of hadicrafts type stands, also a name at each line, following the mentioned rules. This function must call the previous one. For the following example:>>> dic = {'cheese': 'food', 'leather': 'handicraft', ... 'sausages': 'food', 'cakes': 'food', ... 'dresses': 'handicraft', 'jewels': 'handicraft'} >>> stands('stands.txt', 'food.txt', 'craft.txt', dic, 5.1, 6.1)
this function must create two files with the names
'food.txt'and'craft.txt', with the following content:'food.txt' 'craft.txt' joan pera marta perez pepeta morales georgina pi jordi pons
Note
Automatic tests are provided in file
test-stands.txt.
Solution
A solution is provided in file fair.py.