Lab activity

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

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


Set negatives to zero

Write a function setnegatives(values) that:

Introduction to computer programming, 3 CFU (2023-2024)

The function must return nothing, but it must modify the list passed as an argument. In particular, it is possible to write this function without using the return statement. Look in the next example how the function should behave

L = [1.2, -0.4, 5, -2, 12 ]

setnegatives(L)

print(L)
[1.2, 0, 5, 0, 12]

Python code must be saved in file: setnegatives.py

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


List of numbers

Write a function createlist(a, b) that:

For example, the instruction s = crealist1(14, 5) must assign to s the list [14, 15, 16, 17, 18].

s = createlist(14, 5)
print(s)
s = createlist(14, 7)
print(s)
[14, 15, 16, 17, 18]
[14, 15, 16, 17, 18, 19, 20]

Python code must be saved in file: createlist.py

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


Intersection

Write a function intersection(a, b) that:

To get a sorted copy of a list you can call function sorted(...)

For example:

L = intersection([2,4,3,1],[4,1,5])
print(L)
[1, 4]

Python code must be saved in file: intersection.py

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


Local maxima

Write a function localmaxima(seq) that:

An element of a sequence is a local maximum if:

A list containing fewer than three elements contains no local maximum, so an empty list must be returned in this case.

Python code must be saved in file: localmaxima.py

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


Union

Write a function union(a, b) that:

Python code must be saved in file: union.py

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


Test whether a sequence is bitonic

Write a function bitonic(L) that:

A sequence is considered bitonic if it consists of a strictly increasing first part with at least two elements, followed by a strictly decreasing part, also consisting of at least two elements. For example, the following sequences are bitonic:

Observe that, as in the examples, the last element of the increasing part is also the first element of the decreasing part. No bitonic sequence can have less than three elements. In fact, the list

is not bitonic because the segment 6 6 is neither strictly increasing nor decreasing.

print(bitonic([2,4,13,34,23]))
print(bitonic([3,4,1]))
print(bitonic([27,48,113,134,23,12]))
print(bitonic([2,4,6,6,3,-2]))
True
True
True
False

Python code must be saved in file: bitonic.py

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