1.11. Exercises for Lecture 1#
1.11.1. Exercise 1.1#
Write a program that reads an integer given as input from the user and determines whether it is divisible by 2, 3, 5, or 7.
Encapsulate the check into a function taking as input two numbers, and write a program that asks the user to insert two numbers and checks whether the first is divisible by the other one (and vice-versa).
1.11.2. Exercise 1.2#
Write a program that, given the three sides of a triangle, determines whether the triangle is acute-angled, rectangular-angled or obtuse-angled.
1.11.3. Exercise 1.3#
Write a program that, by using a
while
loop, returns the Fibonacci sequence up to the n-th term and stores it in a pythonlist
.
1.11.4. Exercise 1.4#
Write a program that, by using a
for
loop, returns the Fibonacci sequence up to the n-th term and stores it in a pythondictionary
, where thekey
represents the index of each element andvalue
its actual value.
1.11.5. Exercise 1.5#
Define a function that returns the Fibonacci sequence up to the n-th term.
Hint
The function prototype could be
def fibonacci (n) : """A function that calculates the Fibonacci sequence up to the n-th term Args: n (int): the n-th term of the sequence Returns: list: a list with the Fibonacci sequence """
Test the function with a
main
program, filling alist
with the elements of the sequence.Create a new list containing only the elements with even index in the
list
.Create a new list containing only the elements with odd index in the
list
.Move the function in a library and import it in the main program.
1.11.6. Exercise 1.6#
By writing a suitable program and functions, verify whether the value of variables passed to a function get modified in the main program, if they are changed inside the function.
Perform the check for the various types described during the lecture.
1.11.7. Exercise 1.7#
Write a python program that determines the solution of second-order equations
1.11.8. Exercise 1.8#
Write a python program that finds the list of prime integer numbers smaller than 100, starting by knowing that 2 is a prime number
1.11.9. Exercise 1.9#
Write a python program that finds the decomposition in prime factors of a positive integer number, implementing the algorithm in a function encapsulated in a python module
Write a test function, in the library, that checks the correctness of the procedure for all numbers from 1 to 100