More exercises

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

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


Discount price

Write a function discount_price(p, s) that:

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


Factorial

Write a function factorial(x) that:

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


Sort three values

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.

Nested cycles

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

Tree

Write a function tree(x) that:

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


Word count

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

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


Symmetric matrix

Write a function symmetric(M) that,

A matrix is symmetric if and only if M[i][j] == M[j][i] for all i, j.

For example:

M = [[3, 2, 1], [2, 0, 5], [3, 5, 6]] 
print(symmetric(M))
False

Python code must be saved in file: symmetric.py

Test file (download by right click): test_symmetric.py


Remove max frequent element

Write a remove_max_frequent_element(L) function which:

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


Compute moving averages

Write a function moving_average(seq, k) that:

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].

moving_average([2, 4,  3, 6, 8, 9,  10, 12],4)
[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


Check whether all keys in a dictionary are short

Write a function dict_check_short_keys(d, threshold) that:

You may assume that alle keys are strings.

For example:

d = {"Mario": 1000, "Gianna" : 8000, "Alberto": 4000}

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


Sum values associate to keys smaller than k

Write a function dict_sum_small_keys(d, threshold) that:

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:

d = {"Mario": 1000, "Gianna" : 8000, "Alberto": 4000}

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


Union of dictionaries

Write a function dict_union(d1, d2) that:

For example:

d1 = {"January": 10000, "February" : 8000, "March": 4000}
d2 = {"January": 3000, "April" : 2000, "May": 1000}

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


Add union

Write a function add_union(a, b) that:

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