The purpose of the lab is to practice and measure one’s preparation: the exercises are not too difficult, if you have followed the lectures. However, no score is put on them.
Working mode: students should work independently or in small groups, without disturbing colleagues. Group work is fruitful only if everyone participates and if everyone writes their own solution for all exercises.
The teacher will try as much as possible not to occupy the time of the laboratory to introduce new material, although sometimes this will be necessary. The lecturer is available to help students, who can start working even before the lecturer arrives in the classroom, if they wish.
Recommendations: Read the text of the exercises carefully before ask for clarification. In any case, I will be in the classroom with you.
Using test files: To help you complete this exercise you have test programs available to test your solution. These are similar to those you will have on the exam, so I recommend that you learn how to use them. To complete the exercise you will need to
python3 <fileditest>
command in that
folder;where <fileditest>
should obviously be replaced by
the name of the test appropriate for the exercise you are working on.
For each exercise there is an independent test file, so that you can
easily work on the exercises one at a time.
The result of each test is a screen(s) in which it is shown:
For each function written, calls are made with different values of the parameters. The outcome of the tests is reported with the character
E
if the call cannot be executed,F
if the function does not return the correct
result,.
if the function returns the correct result.Write a function create_square_dict(n)
that:
n
;dict
whose keys are nambers 1, 2, 3, …, n-1,
where each key i is associated
to value i * i. You
may assume n is larger than
1.For example:
print(create_square_dict(5)
{1: 1, 2: 4, 3: 9, 4: 16}
Python code must be saved in file:
create_square_dict.py
Test file (download by right click): test_create_square_dict.py
Write a function create_dict_from_lists(n)
that:
k_list
and
v_list
with equal lengths;dict
whose keys are taken from elements in
k_list
and values are taken from v_list
.
Element v_list[0]
will be associated to
k_list[0]
, element v_list[1]
will be
associated to k_list[1]
and so on.For example:
print(create_dict_from_lists(["cat", "dog", "mouse"],
"gatto", "cane", "topo"])) [
{'cat': 'gatto', 'dog': 'cane', 'mouse': 'topo'}
Python code must be saved in file:
create_dict_from_lists.py
Test file (download by right click): test_create_dict_from_lists.py