.. py:module:: fair 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 (:class:`str`), its length (:class:`float`) and the product it offers (:class:`str`), separated by a semicolon (``';'``). In addition, there is a dictionary (:class:`dict`) where the key is the name of a product and the value is its type (both :class:`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: .. literalinclude:: examples/stands.txt :language: text :lines: 1- You can download this example file: :download:`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``. 1. Write function :py:func:`decode` that takes a line of the given file (:class:`str`) and the given dictionary, and returns a (:class:`tuple`) with the name (:class:`str`), lenght (:class:`float`) and product type (:class:`str`) of the stand. Examples: .. literalinclude:: test-decode.txt :language: python3 :lines: 3- .. note:: More tests are provided in file :download:`test-decode.txt`. 2. Write function :py:func:`stands` that takes the names (:class:`str`) of three files (*fstands*, *ffood* and *fcrafts*), a dictionary as the one described and the lengths (:class:`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: .. literalinclude:: test-stands.txt :language: python3 :lines: 14-18 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 :download:`test-stands.txt`. .. rubric:: Solution A solution is provided in file :download:`fair.py`.