Part 2: Exercises

Contents

Part 2: Exercises#

Exercise 10

What is the boolean value of not (not True or False and True) or False?

Exercise 11

What is the boolean value of "spam" not in "spa span sparql" and not ("egg" > "span")?

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.

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.

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.

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.

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?

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")

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)

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)

Exercise 20

Write a sequence of instructions in Python to create a list with the following elements ordered alphabetically: "​Harry"​, "​Draco"​, "​Hermione"​, ​"​Ron"​, "​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").

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().

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.

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.

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.

Exercise 26

Create a test case for the algorithm introduced in a listing of Chapter Brute-force algorithms.

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")

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)?

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")?

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")?

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.

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.

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.

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.

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.

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.

Exercise 37

Write a code in Python to create a set of the following elements: "​Bilbo", "​Frodo", "​Sam", "​Pippin", "​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"}))

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

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])?

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")?

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.

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.

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.

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.

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)?

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.

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.

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)

References#