Part 2: Exercises#
Exercise 10
What is the boolean value of not (not True or False and True) or False?
Solution to Exercise 10
not (not True or False and True) or False =>
not (False or False and True) or False =>
not (False or False ) or False =>
not False or False =>
True or False =>
True
Exercise 11
What is the boolean value of "spam" not in "spa span sparql" and not ("egg" > "span")?
Solution to Exercise 11
"spam" not in "spa span sparql" and not ("egg" > "span") =>
True and not ("egg" > "span") =>
True and not False =>
True and True =>
True
Exercise 12
Following the Python template in Chapter Programming Languages, write in Python the algorithm proposed originally in a figure of Chapter Algorithms as a flowchart (which uses a different approach compared to the one discussed in Chapter Programming Languages), and accompany such code with the related test function and some executions with varying values of input.
Solution to Exercise 12
# Test case for the algorithm
def test_contains_word(first_word, second_word, bib_entry, expected):
result = contains_word(first_word, second_word, bib_entry)
if expected == result:
return True
else:
return False
# Code of the algorithm
def contains_word(first_word, second_word, bib_entry): # input/output: input two words and a bibliographic entry
result = 0 # process: initialize the result value to 0
if first_word in bib_entry: # decision: the first word is in the bibliographic entry
result = result + 1 # process: sum 1 to the result value
if second_word in bib_entry: # decision: the second word is in the bibliographic entry
result = result + 1 # process: sum 1 to the result value
return result # input/output: return the result value
# Three different test runs
print(test_contains_word("Shotton", "Open",
"Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 2))
print(test_contains_word("Citations", "Science",
"Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 1))
print(test_contains_word("References", "1983",
"Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 0))
The source Python file of the code shown above is available as part of the material of the course.
Exercise 13
Write down a small function in Python that takes in two booleans as input and returns True if both are false, otherwise it returns False.
Solution to Exercise 13
# Test case for the function
def test_f(b1, b2, expected):
result = f(b1, b2)
if expected == result:
return True
else:
return False
# Code of the function
def f(b1, b2):
return not (b1 or b2)
# Tests
print(test_f(True, True, False))
print(test_f(True, False, False))
print(test_f(False, True, False))
print(test_f(False, False, True))
The source Python file of the code shown above is available as part of the material of the course.
Exercise 14
Write down a small function in Python that takes in two boolean values and implements the xor operation, which returns True only when one of the input boolean values is True and the other is False, and returns False otherwise.
Solution to Exercise 14
# Test case for the function
def test_xor(b1, b2, expected):
result = xor(b1, b2)
if expected == result:
return True
else:
return False
# Code of the function
def xor(b1, b2):
return (b1 and not b2) or (not b1 and b2)
# Tests
print(test_xor(True, True, False))
print(test_xor(True, False, True))
print(test_xor(False, True, True))
print(test_xor(False, False, False))
The source Python file of the code shown above is available as part of the material of the course.
Exercise 15
Write down a small function in Python that takes two positive integers as input and returns the result of the division (Python operator: /) of the smaller one over the greater one.
Solution to Exercise 15
# Test case for the function
def test_f(i1, i2, expected):
result = f(i1, i2)
if expected == result:
return True
else:
return False
# Code of the function
def f(i1, i2):
if i1 < i2:
return i1 / i2
else:
return i2 / i1
# Tests
print(test_f(2, 4, 0.5))
print(test_f(4, 2, 0.5))
print(test_f(4, 4, 1))
The source Python file of the code shown above is available as part of the material of the course.
Exercise 16
Consider the following snippet of Python code:
def t(x, y):
return x + y - 2
print(t(5, t(3 + 2, 2)))
Which value will be printed on the screen if we execute such code?
Solution to Exercise 16
8
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-plus_minus.py in a shell.
Exercise 17
Consider the following function in Python:
def ni(s1, s2):
if s1 in s2 and s2 in s1:
return False
else:
return True
Write down the value that is returned by the function above when called as follows:
ni("27", "42")
Solution to Exercise 17
True
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-strings_in.py in a shell.
Exercise 18
Consider the following function in Python:
def xor(b1, b2):
if b1 or b2:
return not b1 or not b2
else:
return False
Write down the value that is returned by the function above when called as follows:
xor(True, True)
Solution to Exercise 18
False
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-xor.py in a shell.
Exercise 19
Consider the following Python function:
def f(x, y):
if x <= 0 and y != 0:
return x / y
else:
return y / x
What is the result of the execution of the following call:
f(3, 0)
Solution to Exercise 19
0
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-small_div.py in a shell.
Exercise 20
Write a sequence of instructions in Python to create a list with the following elements ordered alphabetically: "Harry", "Draco", "Hermione", "Ron", "Severus".
Solution to Exercise 20
my_list = list()
my_list.append("Draco")
my_list.append("Harry")
my_list.append("Hermione")
my_list.append("Ron")
my_list.append("Severus")
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-first-list.py in a shell.
Exercise 21
Consider to have a stack obtained by processing, one by one, the elements included in the list of the first exercise, i.e. my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). Describe the status of my_stack after the execution of each of the following operations: my_stack.pop(), my_stack.pop(), my_stack.append("Voldemort").
Solution to Exercise 21
The stack will contain the items "Draco", "Harry", "Hermione", and "Voldemort": deque(["Draco", "Harry", "Hermione", "Voldemort"]).
Exercise 22
Consider to have a queue obtained by processing, one by one, the elements included in the list of the first exercise, i.e. my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). Describe the status of my_queue after the execution of each of the following operations: my_queue.popleft(), my_queue.append("Voldemort"), my_queue.popleft().
Solution to Exercise 22
The queue will contain the items "Hermione", "Ron", "Severus", and "Voldemort": deque(["Hermione", "Ron", "Severus", "Voldemort"]).
Exercise 23
Write down a small function in Python that takes in input two strings and returns -1 if the first string is longer than the second string, 0 if the strings have the same length, and 1 if the second string is longer than the first string.
Solution to Exercise 23
# Test case for the function
def test_f(s1, s2, expected):
result = f(s1, s2)
if expected == result:
return True
else:
return False
# Code of the function
def f(s1, s2):
if len(s1) > len(s2):
return -1
elif len(s1) < len(s2):
return 1
else:
return 0
# Tests
print(test_f("hello", "hi", -1))
print(test_f("hi", "hello", 1))
print(test_f("hello", "earth", 0))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-greater_string.py in a shell.
Exercise 24
Write down a small function in Python that takes in input two strings and returns True if they are identical, False if they are not identical but contains the same number of characters, otherwise it returns the shorter one.
Solution to Exercise 24
# Test case for the function
def test_f(s, t, expected):
result = f(s, t)
if expected == result:
return True
else:
return False
# Code of the function
def f(s, t):
if s == t:
return True
elif len(s) == len(t):
return False
elif len(s) < len(t):
return s
else:
return t
# Tests
print(test_f("ciao", "ciao", True))
print(test_f("ciao", "oaic", False))
print(test_f("ciao", "me", "me"))
print(test_f("me", "ciao", "me"))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-check_string.py in a shell.
Exercise 25
Write down the execution steps of linear_search(list(["Coraline", "American Gods", "The Graveyard Book", "Good Omens", "Neverwhere"]), "The Sandman"), as explained in a listing of Chapter Brute-force algorithms.
Solution to Exercise 25
The instructions in the for-each loop used in the function will be executed five times (i.e. for all the items in input_list), since no item will contain "The Sandman" as value. Thus, None will be returned.
The various iterations of the for-each loop will be as follows:
The variable
positionwill be set to0, and the variableitemwill be set to"Coraline". The condition defined in the if statement will beFalsesince"Coraline"is not equal to"The Sandman", which is the value to search assigned to the variablevalue_to_search. Thus, the return instruction under the if block will not be executed.The variable
positionwill be set to1, and the variableitemwill be set to"American Gods". The condition defined in the if statement will beFalsesince"American Gods"is not equal to"The Sandman", which is the value to search assigned to the variablevalue_to_search. Thus, the return instruction under the if block will not be executed.The variable
positionwill be set to2, and the variableitemwill be set to"The Graveyard Book". The condition defined in the if statement will beFalsesince"The Graveyard Book"is not equal to"The Sandman", which is the value to search assigned to the variablevalue_to_search. Thus, the return instruction under the if block will not be executed.The variable
positionwill be set to3, and the variableitemwill be set to"Good Omens". The condition defined in the if statement will beFalsesince"Good Omens"is not equal to"The Sandman", which is the value to search assigned to the variablevalue_to_search. Thus, the return instruction under the if block will not be executed.The variable
positionwill be set to4, and the variableitemwill be set to"Neverwhere". The condition defined in the if statement will beFalsesince"Neverwhere"is not equal to"The Sandman", which is the value to search assigned to the variablevalue_to_search. Thus, the return instruction under the if block will not be executed.
Since no return instruction will be executed, None will be implicitly returned by the function.
Exercise 26
Create a test case for the algorithm introduced in a listing of Chapter Brute-force algorithms.
Solution to Exercise 26
from collections import deque
# Test case for the function
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if expected == result:
return True
else:
return False
# Code of the function
def stack_from_list(input_list):
output_stack = deque() # the stack to create
# Iterate each element in the input list and add it to the stack
for item in input_list:
output_stack.append(item)
return output_stack
# Three different test runs
print(test_stack_from_list([], deque()))
print(test_stack_from_list([1, 2, 3, 4, 5], deque([1, 2, 3, 4, 5])))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-test_stack_from_list.py in a shell.
Exercise 27
Consider the following function in Python:
def ln(inp, val):
for p, i in enumerate(inp):
if i != val:
return p
Write down the value that is returned by the function above when called as follows: ln(["a", "b", "c"], "b")
Solution to Exercise 27
0
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-falsify_linear_search.py in a shell.
Exercise 28
Consider the following Python function:
def f(n):
result = list()
while n > 0:
result.append(n)
n = n -1
return len(result)
What is the result of the execution of f(3)?
Solution to Exercise 28
3
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-listing_integers.py in a shell.
Exercise 29
Consider the following Python function:
def f(s1, s2):
result = True
for c in s1:
result = result and (c in s2)
return result
What is the result of the execution of f("riddle", "dialer")?
Solution to Exercise 29
True
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-chars_in_strings.py in a shell.
Exercise 30
Consider the following Python function:
def f(x):
r = 0
x_len = len(x)
while x_len > 0:
r = r + x_len
x_len = x_len - 1
return r
What is the result of the execution of f("me")?
Solution to Exercise 30
3
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-working_with_len.py in a shell.
Exercise 31
Write down a small function in Python that takes in input a number and a list of numbers and returns True if the sum of all the numbers in the input list is equal to the input number, otherwise it returns False.
Solution to Exercise 31
# Test case for the function
def test_f(n, n_list, expected):
result = f(n, n_list)
if expected == result:
return True
else:
return False
# Code of the function
def f(n, n_list):
result = 0
for i in n_list:
result += i
return result == n
# Tests
print(test_f(10, [1, 2, 3, 4], True))
print(test_f(10, [0, 1, 2, 3, 4], True))
print(test_f(10, [1, 2, 3], False))
print(test_f(10, [1, 2, 3, 4, 5], False))
print(test_f(10, [], False))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-sum-numbers.py in a shell.
Exercise 32
Write down a small function in Python that takes in input a string and a boolean and return a list of the vowel characters (i.e. those matching with any of the following ones: "a", "e", "i", "o", "u") in the input string if the input boolean is True, otherwise (i.e. the input boolean is False) it returns a list of the characters that are not vowels.
Solution to Exercise 32
# Test case for the function
def test_f(s, b, expected):
result = f(s, b)
if expected == result:
return True
else:
return False
# Code of the function
def f(s, b):
result = list()
for c in s:
if b and c in "aeiou":
result.append(c)
elif not b and c not in "aeiou":
result.append(c)
return result
# Tests
print(test_f("john doe", True, ["o", "o", "e"]))
print(test_f("john doe", False, ["j", "h", "n", " ", "d"]))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-check_vowels.py in a shell.
Exercise 33
Write down a small function in Python that takes in three strings as input and returns a tuple of two items containing the two longest input strings.
Solution to Exercise 33
# Test case for the function
def test_f(s1, s2, s3, expected):
result = f(s1, s2, s3)
if sorted(expected) == sorted(result):
return True
else:
return False
# Code of the function
def f(s1, s2, s3):
l_s1 = len(s1)
l_s2 = len(s2)
l_s3 = len(s3)
if l_s1 <= l_s2 and l_s1 <= l_s3:
return s2, s3
elif l_s2 <= l_s1 and l_s2 <= l_s3:
return s1, s3
else:
return s1, s2
# Tests
print(test_f("ciao", "hello", "hi", ("ciao", "hello")))
print(test_f("ciao", "hi", "hi", ("ciao", "hi")))
print(test_f("hi", "hi", "hi", ("hi", "hi")))
print(test_f("hi", "hi", "ciao", ("hi", "ciao")))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-measuring_strings.py in a shell.
Exercise 34
Write in Python the function def my_enumerate(input_list) which behaves like the built-in function enumerate() introduced in Section “Linear search” of Chapter Brute-force algorithms and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function enumerate() in the implementation.
Solution to Exercise 34
# Test case for the function
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if expected == result:
return True
else:
return False
# Code of the function
def my_enumerate(input_list):
l = list()
for i in range(len(input_list)):
l.append((i, input_list[i]))
return l
# Tests
print(test_my_enumerate([], []))
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")]))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-my_enumerate.py in a shell.
Exercise 35
Write in Python the function def my_range(stop_number) which behaves like the built-in function range() introduced in Section “Insertion sort” of Chapter Brute-force algorithms and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function range() in the implementation.
Solution to Exercise 35
# Test case for the function
def test_my_range(stop_number, expected):
result = my_range(stop_number)
if expected == result:
return True
else:
return False
# Code of the function
def my_range(stop_number):
l = list()
while stop_number > 0:
stop_number = stop_number - 1
l.insert(0, stop_number)
return l
# Tests
print(test_my_range(0, []))
print(test_my_range(1, [0]))
print(test_my_range(4, [0, 1, 2, 3]))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-my_range.py in a shell.
Exercise 36
Write in Python the function def my_reversed(input_list) which behaves like the built-in function reversed() introduced in Section “Insertion sort” of Chapter Brute-force algorithms and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function reversed() in the implementation.
Solution to Exercise 36
# Test case for the function
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if expected == result:
return True
else:
return False
# Code of the function
def my_reversed(input_list):
l = list()
for item in input_list:
l.insert(0, item)
return l
# Tests
print(test_my_reversed([], []))
print(test_my_reversed([1], [1]))
print(test_my_reversed([1, 2, 4, 3, 4, 7, 2], [2, 7, 4, 3, 4, 2, 1]))
print(test_my_reversed(["a", "b", "c", "d"], ["d", "c", "b", "a"]))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-my_reversed.py in a shell.
Exercise 37
Write a code in Python to create a set of the following elements: "Bilbo", "Frodo", "Sam", "Pippin", "Merry".
Solution to Exercise 37
my_set = set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Pippin")
my_set.add("Merry")
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-create_set.py in a shell.
Exercise 38
Consider the set created in the first exercise, stored in the variable my_set. Describe the status of my_set after the execution of each of the following operations:
my_set.remove("Bilbo")
my_set.add("Galadriel")
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
Solution to Exercise 38
The set will contain the elements "Frodo", "Sam", "Pippin", "Merry", "Galadriel", "Saruman", "Gandalf".
Exercise 39
Suppose to organise some of the elements in the set returned by the second exercise in two different sets: set_hobbit that refers to the set set({"Frodo", "Sam", "Pippin", "Merry"}), and set_magician defined as set({"Saruman", "Gandalf"}). Create a dictionary containing two pairs: one that associates the set of hobbits with the key "hobbit", and the other that associates the set of magicians with the key "magician".
Solution to Exercise 39
set_hobbit = set()
set_hobbit.add("Frodo")
set_hobbit.add("Sam")
set_hobbit.add("Pippin")
set_hobbit.add("Merry")
set_magician = set()
set_magician.add("Saruman")
set_magician.add("Gandalf")
my_dict = dict()
my_dict["hobbit"] = set_hobbit
my_dict["magician"] = set_magician
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-create_dict_of_sets.py in a shell.
Exercise 40
Consider the following Python function:
def g(x):
r = set()
idx = 0
for it in x:
if it not in r:
r.add(idx)
idx = idx + 1
return r
What is the result of the execution of g([5, 7, 7, 2, 5, 7])?
Solution to Exercise 40
{0, 1, 2, 4, 5}
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-set_of_integers.py in a shell.
Exercise 41
Consider the following Python function:
def g(s):
result = dict()
for c in s:
if c not in result:
result[c] = 0
result[c] = result[c] + 1
return result.get("o")
What is the result of the execution of g("Bologna")?
Solution to Exercise 41
2
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-dict_of_integer_values.py in a shell.
Exercise 42
Write down a small function in Python that takes in input two strings and returns a set of all the characters they have in common.
Solution to Exercise 42
# Test case for the function
def test_f(s1, s2, expected):
result = f(s1, s2)
if expected == result:
return True
else:
return False
# Code of the function
def f(s1, s2):
result = set()
for c in s1:
if c in s2:
result.add(c)
return result
# Tests
print(test_f("anna", "elsa", {"a"}))
print(test_f("ron", "hermione", {"r", "o", "n"}))
print(test_f("", "hello", set()))
print(test_f("dad", "mum", set()))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-set_of_matched_chars.py in a shell.
Exercise 43
Write down a small function in Python that takes in two strings as input and returns the set of all the digit characters they do not have in common.
Solution to Exercise 43
# Test case for the function
def test_f(s, n, expected):
result = f(s, n)
if expected == result:
return True
else:
return False
# Code of the function
def f(s1, s2):
result = set()
for d in "0123456789":
if (d in s1 and d not in s2) or (d not in s1 and d in s2):
result.add(d)
return result
# Tests
print(test_f("alice", "bob", set()))
print(test_f("2 books and 1 pen", "trees and 2 apples", {"1"}))
print(test_f("odd number: 1, 3, 5, 7, 9", "even number: 2, 4, 6, 8", {"1", "2", "3", "4", "5", "6", "7", "8", "9"}))
print(test_f("odd number: 1, 3, 5, 7, 9", "prime number: 1, 2, 3, 5, 7", {"2", "9"}))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-set_digits.py in a shell.
Exercise 44
Write down a small function in Python that takes in two strings as input and returns a set containing the characters that are not contained in both strings.
Solution to Exercise 44
# Test case for the function
def test_f(s1, s2, expected):
result = f(s1, s2)
if expected == result:
return True
else:
return False
# Code of the function
def f(s1, s2):
result = set()
for c in s1 + s2:
if not (c in s1 and c in s2):
result.add(c)
return result
# Tests
print(test_f("", "", set()))
print(test_f("hello", "hello", set()))
print(test_f("hello", "", {"h", "e", "l", "o"}))
print(test_f("", "hello", {"h", "e", "l", "o"}))
print(test_f("hello", "hi", {"i", "e", "l", "o"}))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-chars_not_in_both_strings.py in a shell.
Exercise 45
Write down a small function in Python that takes in two strings as input and returns the number of characters the two strings have in common.
Solution to Exercise 45
# Test case for the function
def test_f(s1, s2, expected):
result = f(s1, s2)
if expected == result:
return True
else:
return False
# Code of the function
def f(s1, s2):
result = set()
for c in s1:
if c in s2:
result.add(c)
return len(result)
# Tests
print(test_f("hello", "loch", 3))
print(test_f("hello", "hi", 1))
print(test_f("hello", "hello", 4))
print(test_f("hello", "try", 0))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-dev-chars_in_both_strings.py in a shell.
Exercise 46
Consider the following Python function:
def f(s1, s2, n):
if s1 < s2:
return n
else:
return f(s2, s1, n * -1)
What is the result of the execution of f("mickey","donald",7)?
Solution to Exercise 46
-7
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-recursive_check_strings.py in a shell.
Exercise 47
Define a recursive function def exponentiation(base_number, exponent) for implementing the exponentiation operation. Test (by implementing the related test case) it on the following inputs: 34, 171, and 20.
Solution to Exercise 47
# Test case for the function
def test_exponentation(base_number, exponent, expected):
result = exponentation(base_number, exponent)
if expected == result:
return True
else:
return False
# Code of the function
def exponentation(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * exponentation(base_number, exponent - 1)
# Tests
print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(2, 0, 1))
print(test_exponentation(0, 15, 0))
print(test_exponentation(0, 0, 1))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-exponentation.py in a shell.
Exercise 48
Define a recursive function def fib(n) that implements the algorithm to find the nth Fibonacci number. In particular, if n is less than or equal to 0, then 0 is returned as a result. Otherwise, if n is equal to 1, then 1 is returned. Otherwise, return the sum of the same function called with n-1 and n-2 as input. Please accompany the function with the related test case.
Solution to Exercise 48
# Test case for the function
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
# Code of the function
def fib(n):
if n <= 0:
return 0
if n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
# Tests
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(7, 13))
print(test_fib(-15, 0))
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-fib_recursive.py in a shell.
Exercise 49
The variable my_mat_list is a list of the ten integer numbers (each one can assume a value between 0 and 9, inclusive), and the variable my_n_odd is the number of odd numbers in the list. Study the execution of the following function passing my_mat_list and my_n_odd, as input (i.e. f(my_mat_list, my_n_odd)).
def f(mat_list, n_odd):
if n_odd <= 0 or len(mat_list) == 0:
return 0
else:
v = 0
result = list()
for i in mat_list:
if v > 0:
result.append(i)
if i > 0 and v == 0:
v = i
return v + f(result, n_odd - 1)
Solution to Exercise 49
The function f is a recursive function which act on a selection of the numbers included in the input mat_list parameter. In particular, 0 is returned either in the case it does not contain any odd number or there are no more items in the input list.
In case these two cases do not apply, a recursive step is executed. Before executing the recursion, though, a list (stored in the variable result) of numbers greater than 0 is created as a result of the execution of a for each loop that parses all the numbers in the input list. In addition, as a consequence of this loop, a variable v is also initialised and contains the first positive number encountered while parsing the input list.
Then, the recursive step is applied. In particular, the code returns the sum of the content of the variable v just created with the result of the execution of the function f passing the variable result and the number of odd numbers provided in input minus 1 as input.
For instance, the execution of the function
f([0, 0, 0, 1, 0, 0, 5, 2, 0, 0], 2)
proceeds as follows:
According to the input specified, the
elseblock is executed. After the execution of the for each loop, the variablevis set to1while theresultlist contains[0, 0, 5, 2, 0, 0]. Then, the first recursive call is executed passing the list inresultand1as input.The first recursive call is executed and, since the condition of the
ifblock isFalse, the execution continues to theelseblock. After the execution of the for each loop, the variablevis set to5and theresultlist contains[2, 0, 0]. Then, the second recursive call is executed passing the list inresultand0as input.The second recursive call is executed and, since the condition of the
ifblock isTrue(i.e.n_oddis equal to0),0is returned by the function.The first recursive call receives back from the second recursive call the result of its execution (i.e.
0). It sums it with the value inv(i.e.5) and returns the final result (i.e.5).The initial function
freceives back from the first recursive call the result of its execution (i.e.5). It sums it with the value inv(i.e.1) and returns the final result (i.e.6).
The source Python file of the code shown above is available as part of the material of the course. You can run it executing the command python ex-und-odd_numbers_in_matriculation.py in a shell.