Luiss
Libera Università Internazionale
degli Studi Sociali Guido Carli
Digital Transformation
and Emerging
Technologies
Coding Lab 8
Exercise 17
• Define the check_difference function that takes a list of numbers
as parameters and returns True or False according to whether all
the numbers are different from each other.
Solution
In this function, unique_numbers is a list
used to store unique numbers encountered
while iterating through the input list numbers.
For each number in numbers, the function
checks if it's already in unique_numbers. If it
is, the function immediately returns False
because a duplicate has been found. If the
number is not in unique_numbers, it is added
to the list. After checking all numbers, if no
duplicates are found, the function returns True.
This approach uses a for loop and a list to
efficiently check for duplicates and determine
if all numbers in the input list are different
from each other.
Exercise 18
• Define the multiply_list function that takes as parameters a list of
numbers and a number n and returns a new list with each element
multiplied by n.
Solution
Exercise 18 a
• Write a function generate_odd_squares(n) that takes an integer
n as input and returns a list containing the squares of all odd
numbers from 1 to n using a while loop.
Solution
Exercise 18 b
• Write a function calculate_factorial(n) that takes an integer n as
input and returns the factorial of n using a while loop.
• Ex. FACTORIAL (4) = 4x3x2x1
Solution
Exercise 18 c
• Write a function generate_fibonacci_sequence(n) that takes an integer
n as input and returns a list containing the first n numbers in the
Fibonacci sequence with for cycle
HINT: The Fibonacci Sequence is a series of numbers. The next number
is found by adding up the two numbers before it. The first two
numbers are 0 and 1.
For example, 0, 1, 1, 2, 3, 5, 8, 13, 21. The next number in this series
above is 13+21 = 34.
Solution
Exercise 19
• Write the sep_pos_neg function that takes a list of positive and
negative numbers as parameters and returns two new list, one
containing positive numbers and one the negative numbers
Solution
Exercise 20
• Define the palindrome_list function that given a list of words as
parameter returns the list of only words that are palindrome.
• HINTS: use [::-1] for obtaining the reverse of the words
Solution
Exercise 21
• Define a function that finds all such numbers which are divisible
by 7 but are not a multiple of 5, between 100 and 200 (both
included) and returns a list containing them.
Solution