Recommendations: Read the text of the exercises carefully.
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 discount_price(p, s)
that:
p
and a discount percentage
s
, which are two numbers.p
or s
are negative, return
None
.See the documentation for the round
function
Examples: - discount_price(100, 10)
returns
90
. - discount_price(-100, 10)
returns the
value None
.
The function must be saved in a file called
discount_price.py
.
Python code must be saved in file: discount_price.py
Test file (download by right click): test_discount_price.py
Write a function factorial(x)
that:
x
;x
, i.e., the product of the
first x
natural numbers.Esempi: - factorial(3)
returns 6
-
factorial(5)
returns 120
.
The function must be saved in a file named
factorial.py
Python code must be saved in file: factorial.py
Test file (download by right click): test_factorial.py
Write a program that prints three values passed as arguments in order
from smallest to largest. For example, if the three values are
5.2, 1.4, 2.2
should print out in order
1.4, 2.2, 5.2
.
The program must read three values A, B, C from the keyboard, and then print them out in ascending order.
Write a function that prints the following characters.
* ** *** **** ***** ****** ******* ******** ********* **********
Generalize the function so that the triangle height is a parameter. If the parameter value is 4, you should print
* ** *** ****
Suggestion: try printing:
XXX* XX** X*** ****
then change X
to a blank space
Write a function tree(x)
that:
x
;You can insert the special character \n
in the string to
cause newline.
Examples: - tree(3)
returns
' *\n ***\n*****\n'
Function must be saved in file tree.py
print(tree(7))
* *** ***** ******* ********* *********** *************
Python code must be saved in file: tree.py
Test file (download by right click): test_tree.py
Write a function word_count(text)
that returns the
number of words in string text
. E.g.:
print(word_count("Python4Dummy? Is the worst book on Python."))
7
Note that Python4Dummy
is a single word. We consider as
a word any sequence of uppercase or lowercase letters and digits. Spaces
and punctuation are considered as separators between words.
You may use methods
str.isalpha()
that returns True
when a
string is not empty and contains only letters;str.isdigit()
that returns True
when a
string is not empty and contains only digits.In oparticular, you may use these methods to check wheter a single character is a letter or a digit.
For example:
print('B'.isalpha())
print("3".isdigit())
print("a,b".isalpha())
print("Paul".isalpha())
print("342".isdigit())
print("Paul!".isalpha())
print("3 42".isdigit())
True
True
False
True
True
False
False
Python code must be saved in file: word_count.py
Test file (download by right click): test_word_count.py
Write a function symmetric(M)
that,
M
,
represented as a list of lists;True
if M
is a symmetric matrix,
False
otherwise.A matrix is symmetric if and only if M[i][j] == M[j][i]
for all i, j
.
For example:
= [[3, 2, 1], [2, 0, 5], [3, 5, 6]]
M print(symmetric(M))
False
Python code must be saved in file: symmetric.py
Test file (download by right click): test_symmetric.py
Write a remove_max_frequent_element(L)
function
which:
L
as a parameter,L
by removing all
occurrences of the element that appears with the maximum frequency. If
the list is empty, modify nothing.Assume that there are no equalities, i.e. that the most frequent element is unique in the list.
Python code must be saved in file:
remove_max_frequent_element.py
Test file (download by right click): test_remove_max_frequent_element.py
Write a function moving_average(seq, k)
that:
seq
and an integer
k
;ValueError
if the list has zero
length;ValueError
if
0<k<=len(seq)
does not hold;len(seq)-k+1
moving averages
of order k
.A moving average of order k
is the arithmetic average of
k
consecutive elements in the sequence. If the sequence has
lenght n
we can compute n-k+1
moving averages:
from the subsequence [0:k]
to the subsequence
[n-k:n]
(left element included, right element not
included). Note that the number of moving averages depends both on
n
and on k
.
For example, if function moving_average(seq, k)
is
called with parameters seq = [2, 4, 3, 6, 8, 9, 10, 12]
and k = 4
, the function must return
[3.75, 5.25, 6.5, 8.25, 9.75]
, that is the averages
computed on the 5 subsequences [2, 4, 3, 6]
,
[4, 3, 6, 8]
, [3, 6, 8, 9]
,
[6, 8, 9, 10]
, [8, 9, 10, 12]
.
2, 4, 3, 6, 8, 9, 10, 12],4) moving_average([
[3.75, 5.25, 6.5, 8.25, 9.75]
Python code must be saved in file: moving_average.py
Test file (download by right click): test_moving_average.py
Write a function dict_check_short_keys(d, threshold)
that:
d
with keys of type
str
and an integer threshold
;True
if all keys have length smaller or equal
to threshold
, and returns False
otherwise.You may assume that alle keys are strings.
For example:
= {"Mario": 1000, "Gianna" : 8000, "Alberto": 4000}
d
print(dict_check_short_keys(d, 6))
False
Python code must be saved in file:
dict_check_short_keys.py
Test file (download by right click): test_dict_check_short_keys.py
Write a function dict_sum_small_keys(d, threshold)
that:
d
with numeric values
and a key threshold
;threshold
.You may assume that all values in the dictionary are numeric. If the
dictionary is empty or it does not contain any key smaller than
threshold
, the functin must return 0.
For example:
= {"Mario": 1000, "Gianna" : 8000, "Alberto": 4000}
d
print(dict_sum_small_keys(d, "Lucia"))
12000
Because keys Alberto
and Gianna
are smaller
than Lucia
in lexicografic order, while key
Mario
is larger.
Python code must be saved in file:
dict_sum_small_keys.py
Test file (download by right click): test_dict_sum_small_keys.py
Write a function dict_union(d1, d2)
that:
d1
e d2
as
arguments, with numeric values;dict
where keys are taken from keys in
d1
and keys in d2
. If a key is only in
d1
or only in d2
the associated value will be
set in the returned dictionary. If a key is both in d1
and
in d2
the value in teh returned dictionary will be the sum
of the two values in d1
and d2
.For example:
= {"January": 10000, "February" : 8000, "March": 4000}
d1 = {"January": 3000, "April" : 2000, "May": 1000}
d2
print(dict_union(d1, d2))
{'January': 13000, 'February': 8000, 'March': 4000, 'April': 2000, 'May': 1000}
Python code must be saved in file: dict_union.py
Test file (download by right click): test_dict_union.py
Write a function add_union(a, b)
that:
a
, b
as arguments;a
adding all elements that appear in
b
and do not appear in a
, so that
a
is the union of a
and b
.You can assume that a
and b
do not contain
duplicates. Anyway, some elemnts may appear both in a
and
in b
. The function does not return any value, but leaves
list a
modified.
Elements must be added at the end of a
in the same order
n which they appear in b
.
Python code must be saved in file: add_union.py