Second Exam 2022 - 2023 (W - Solutions) - V1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Number: ___________ Name: ______________ Surname: __________________

Computation II
Second Examination Epoch
NOVA-IMS, 2023

Rules:
1. Write in CAPITAL letters and in a clear and readable form your student number, first name and surname
in the header of each page. Anonymous tests, in which the name or the student number is not clear or
ambiguous will not be evaluated.
2. The duration of this test is one 1h and the time of termination will be written by the professor at the
board before the beginning of this test.
3. It is strictly forbidden to detach the staple from the pages’ collection.
4. This test contains four groups of open questions. It is the student’s responsibility to provide a clear and
readable answer. Ambiguous answers will not be evaluated.
5. Provide your answer in the right place: either between two dedicated horizontal lines or in the dedicated
underlined part (e.g. _____________________).
6. The answers to the questions must necessarily be written with a pen on the pages of this document. No
other paper or document will be evaluated.
7. A draft page is provided at the end of this document. It will not be evaluated.
8. It is strictly forbidden to use any kind of documentation (books, notes, slides, etc.), to communicate or
exchange anything with another student and to use any kind of computer.
9. Portable telephones must mandatorily be switched off before the beginning of the test and should be left
in the bag.
10. The use of electronic watches and calculators is not allowed.
11. Not respecting one or more of the previous rules will imply immediate expulsion from the room, the
invalidation of the test and the application of the appropriate disciplinary sanctions.
Number: ___________ Name: ______________ Surname: __________________

1. Mathematical foundations
1.1. Given that 𝑙𝑜𝑔2(16) = 4, 𝑙𝑜𝑔2(8) = 3, 𝑙𝑜𝑔2(4) = 2, 𝑙𝑜𝑔2(1) = 0, what will be
the 𝑙𝑜𝑔2(− 4)?

NOTE: simply provide the answer. No demonstrations are required.

Answer: the function is not defined for x = -4

1.2. Given a summation, you often wish to replace it with a direct equation with the
same value as the summation. This is known as a closed-form solution. What will be
the closed form of the following summation?
NOTE: simply providing the result is not enough. You must prove your rationale. Use the
space between the two lines below to provide an analytical demonstration.

𝑛
Answer: ∑ 0. 33 = 0. 33(𝑛 − 𝑥 + 1)
𝑖=𝑥

Assuming x=4 and n=10:

𝑛
∑ 0. 33 = 0. 33 𝑥=4 + 0. 33𝑥=5 + 0. 33𝑥=6 + 0. 33𝑥=7 + 0. 33𝑥=8 + 0. 33𝑥=9 + 0. 33𝑥=10 = 0. 33 * 7
𝑖=𝑥

= 0. 33 (10 − 4 + 1)

=> 0. 33(𝑛 − 𝑥 + 1)

1.3. What will be the closed form of the following summation?


NOTE: simply providing the result is not enough. You must prove your rationale. Use the
space between the two lines below to provide an analytical demonstration.

𝑛
2
∑ (2𝑖 − 10) = 𝑛 − 9𝑛
𝑖=1

𝑛 𝑛 𝑛
∑ (2𝑖 − 10) = 2 ∑ 𝑖 − ∑ 10 = 2𝑛(𝑛 + 1)/2 − 10𝑛 = 𝑛(𝑛 + 1) − 10𝑛 = 5 + 𝑛 − 10𝑛 =
𝑖=1 𝑖=1 𝑖=1

2
= 𝑛 − 9𝑛
Number: ___________ Name: ______________ Surname: __________________

2. Sort
2.1. Insertion sort assumes that the first element in an array is already sorted; this
represents the “sorted” part of the array. Then, the algorithm iterates over the
remaining “unsorted” array and compares each element to its predecessors in the
“sorted” part. If a given element is smaller than a given predecessor, then it is placed
in the proper position of the “sorted” array.
Consider the following implementation of the insertion sort algorithm. Complete the
lines of code that are missing using Python programming language.
def swap(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp

def sort_insertion(array):
compares, swaps = 0, 0
for i in range(1, len(array)):
for j in range(i, 0, -1):
compares += 1
if array[j] < array[j-1]:
swaps += 1
swap(array, j, j-1)

return compares, swaps

>>> import numpy as np


>>> array = np.array([40, 74, 35, 48, 10, 23, 85, 19, 39, 9])
>>> compares, swaps = sort_insertion(array )
>>> print("Sorted array:", array)
Sorted array: [ 9 10 19 23 35 39 40 48 74 85]
>>> print("Total: compares={} \t swaps={}".format(compares, swaps))
Total: compares=45 swaps=30

2.2. Given the code in the sort_insertion function, how many comparisons (aka
compares) and swaps will be required to sort the input array defined below?
>>> array = np.array([9, 10, 19, 23, 85, 39, 40, 48, 74, 35])
>>> compares, swaps = sort_insertion(array)
>>> print("Sorted array:", array)
Sorted array: [ 9 10 19 23 35 39 40 48 74 85]
>>> print("Total: compares={} \t swaps={}".format(compares, swaps))
Total: compares=45 swaps=9
Number: ___________ Name: ______________ Surname: __________________

2.3. Unnecessary comparisons can be avoided, optimising, therefore the insertion sort
algorithm. If the largest (outermost) value in the ‘sorted’ array is smaller than the first value in
the ‘unsorted’ array, then one can simply expand the ‘sorted’ array by including that value.
This becomes particularly useful when the input array is nearly sorted. Consider a more
efficient version of the insertion sort that takes that into account. Complete the lines of code
that are missing using Python programming language.
NOTE: as you can see, the first four missing lines of code to fill in are exactly the same as in
the previous exercise. Therefore, in this exercise, only the one belonging to the else
statement will be evaluated.

def sort_insertion_(array):
compares, swaps = 0, 0
for i in range(1, len(array)):
for j in range(i, 0, -1):
compares += 1
if array[j] < array[j-1]:
swaps += 1
swap(array, j, j-1)
else:
break

return compares, swaps

>>> array = np.array([9, 10, 19, 23, 85, 39, 40, 48, 74, 35])
>>> compares, _ = sort_insertion_while(array)
>>> print("Sorted array:", array)
Sorted array: [ 9 10 19 23 35 39 40 48 74 85]
>>> print("Total: compares={}".format(compares))
Total: compares=18
Number: ___________ Name: ______________ Surname: __________________

3. Time complexity
3.1. What is the worst-case time complexity of the insertion sort?
NOTE: simply provide the answer. No demonstrations are required.

2
𝑂(𝑛 )

3.2. The merge sort algorithm is an instance of the so-called divide and conquer
algorithms. The basic premise is that we divide a problem into two pieces. Each of
the two pieces is easier to solve than trying to tackle the whole problem at once
because the two pieces are each smaller.
Specifically, merge sort divides the list into two parts, then divides each resulting
part again and again, until the lists are no longer divisible (i.e., of size 1). Notice that
a sublist of length 1 is already sorted (similarly to the starting point of the insertion
sort). Two sorted sublists can be then merged into one sorted list.
Fill in the missing parts in the sentences from the bullet list.
● A list can be divided into lists of size 1 by repeatedly splitting in 𝑂(𝑙𝑜𝑔(𝑛)) time
● Each of the split lists is then merged together in 𝑂(𝑛) time
● This results in a complexity of 𝑂(𝑛 𝑙𝑜𝑔(𝑛)) for the merge sort
● Which algorithm is more efficient assuming a worst-case scenario: merge or insertion
sort?

○ R: merge

● Give an example of an array of length 6 that would represent a worst-case scenario


for a given sorting algorithm

○ R: [ __6__, __5__, __4__, __3__, __2__, __1__]


Number: ___________ Name: ______________ Surname: __________________

3.3. The quick sort algorithm is also an instance of the so-called divide and conquer
algorithms - algorithms that divide a problem into smaller pieces to solve it. But,
unlike the merge sort, which splits the array into the half-index, the quick sort
partitions the array by dividing it into two segments based on a selected value -
called pivot. Consider the following pseudo-code:

● Pick an element from the array (the pivot)


● Reorder the array around pivot:
○ Place all elements that are smaller or equal to the pivot before (i.e., to
the left)
○ Place all elements that are greater than the pivot after (i.e., to the
right)
○ Place the pivot in the right position of the ‘sorted’ array
○ Return the new index of the pivot
● Recursively apply the above steps to the two sub-arrays

2
The worst-case time-complexity of the quick sort is 𝑂(𝑛 ) and the average-case is
𝑂(𝑛 𝑙𝑜𝑔(𝑛)), being the latter equal to that of merge sort. What is then the major
advantage of the quick sort over the merge sort? Use not more than three lines to
answer this question.

Any of the two can be considered correct:

R: The quick sort does not require a temporary array to store the results (i.e., quick sort is
an in-place algorithm).

R (option 2): the quick sort works better for smaller arrays.
Number: ___________ Name: ______________ Surname: __________________

4. Data structures
4.1. “You can undo, redo, or repeat many actions in Microsoft Word, PowerPoint,
and Excel. You can undo changes, even after you have saved, and then save again,
as long as you are within the undo limits (By default Office saves the last 100
undoable actions).“ - Undo, redo, or repeat an action, Microsoft 365 support.
Assume you were hired by Microsoft to implement a data structure that allows you
to store the written text in the Microsoft Office 365 family of products (Word, Excel,
PowerPoint, etc.), and is particularly suitable to handle the frequent undo operations
required by the user. Which data structure would you use and why? Provide your
answer in not more than four lines of written text.
R (data structure): Stacks

WHY:

Stacks have LIFO (Last in First Out) as their working principle. When you undo, the last
change you made is read and removed from the top of the stack and then used to revert your
changes.
Number: ___________ Name: ______________ Surname: __________________

Draft

You might also like