Final Openenrollement S22 230
Final Openenrollement S22 230
The duration of this exam is 2 hours. Keep in mind that you need around 10 minutes at the end
of the exam to submit your answers.
The exam consists of 4 problems for 160 points.
You are only allowed to use the internet to access Moodle and code visualizer. In particular, you
are allowed to access the following material available on Moodle: slides and their associated code,
solving sessions, assignments and their solutions.
You are not allowed to use previously stored materials on your machine.
You are asked to submit a single compressed .zip or .rar file containing your Python files (ending with
.py extension). Name the compressed file according to the format firstName lastName Final.zip (or
.rar), where firstName and lastName are your first name and last name, respectively, without white
spaces. In particular, do not use in the compressed file name white spaces in between characters or
trailing white spaces before the file extension.
It is your responsibility to make sure your files are properly submitted.
Failure to do so may lead to a failing grade on the exam.
If you violate the above rules or if you communicate with a person other than the exam proctors
during the exam, you will immediately get zero.
The problems are of varying difficulty. Below is a rough ordering estimate of the problems in order
of increasing difficulty.
– Level 1-2 (120 points): Problems 1,2,3
– Level 3 (40 points): Problem 4
Detailed comments are worth partial credit.
Plan your time wisely. Do not spend too much time on any one problem. Read through all of them
first and attack them in the order that allows you to make the most progress.
Good luck!
1
Problem 1 (40 points). Number of common elements
Implement the function numOfCommon(A,B), which given two lists of numbers A and B, finds the
number of common elements between A and B. Assume in your function that all the elements of input
list A are distinct and that all the elements of input list B are distinct.
Any correct solution is worth 30 points. For full grade, solve it in O(n log n) time or O(n) expected
time, where n = len(A) + len(B).
Test program/output:
print(numOfCommon([5,1],[])) 0
print(numOfCommon([5,1],[10,17])) 0
print(numOfCommon([5,1],[1,17])) 1
print(numOfCommon([5,1],[1,5])) 2
print(numOfCommon([5,1],[1,5,3])) 2
print(numOfCommon([10,3,12,5,-7,1],[15,20,1,5,33,3])) 3
Overloaded binary operator add (self,offset), where offset is of type float or int, to shift the
interval by the given offset. That is, if I is a timeInterval, then I+offset is the time interval
whose start is I.start+offset and end is I.end+offset. This method should return the exception
"Incorrect type!" if the type of offset is not float or int.
I = timeInterval(1,2) [1,2]
J = timeInterval(1.5,5) [1.5,5]
print(I) [4.5,8]
print(J) False
K = J+3 True
print(K)
print(disjoint(I,J))
print(disjoint(I,K))
2
Problem 3 (40 points). Check if matrix is in increasing row and column orders.
Implement the function increasingRowsAndCols(M), which given an m × n matrix M , returns True if if
i) the entries of each row of M are in increasing order, and
See the the below examples. Note that the third column of M3 is not in increasing order. In M4, the
first row is not in increasing order.
Any correct solution is worth 30 points. For full grade, solve it in O(mn) time.
Test program:
import numpy as np
Output:
M1:
[[1 2]
[2 3]]
True
M2:
[[1 2 4]
[2 3 7]]
True
M3:
[[1 2 8]
[2 3 7]]
False
M4:
[[1 0 4]
[2 3 7]]
False
3
L does not have contiguous zeros, i.e., if L[i] = 0 then L[i + 1]! = 0, for i = 0, . . . , n − 2.
for n in range(5):
print("genLists("+str(n)+")")
print(genLists(n),"\n")
Output:
genList(0)
[[]]
genList(1)
[[-1], [0], [1]]
genList(2)
[[-1, -1], [-1, 1], [-1, 0], [0, -1], [0, 1], [1, -1], [1, 1], [1, 0]]
genList(3)
[[-1, -1, -1], [-1, -1, 1], [-1, -1, 0], [-1, 1, -1], [-1, 1, 1], [-1, 1, 0],
[-1, 0, -1], [-1, 0, 1], [0, -1, -1], [0, -1, 1], [0, -1, 0], [0, 1, -1],
[0, 1, 1], [0, 1, 0], [1, -1, -1], [1, -1, 1], [1, -1, 0], [1, 1, -1],
[1, 1, 1], [1, 1, 0], [1, 0, -1], [1, 0, 1]]
genList(4)
[[-1, -1, -1, -1], [-1, -1, -1, 1], [-1, -1, -1, 0], [-1, -1, 1, -1],
[-1, -1, 1, 1], [-1, -1, 1, 0], [-1, -1, 0, -1], [-1, -1, 0, 1],
[-1, 1, -1, -1], [-1, 1, -1, 1], [-1, 1, -1, 0], [-1, 1, 1, -1],
[-1, 1, 1, 1], [-1, 1, 1, 0], [-1, 1, 0, -1], [-1, 1, 0, 1],
[-1, 0, -1, -1], [-1, 0, -1, 1], [-1, 0, -1, 0], [-1, 0, 1, -1],
[-1, 0, 1, 1], [-1, 0, 1, 0], [0, -1, -1, -1], [0, -1, -1, 1],
[0, -1, -1, 0], [0, -1, 1, -1], [0, -1, 1, 1], [0, -1, 1, 0],
[0, -1, 0, -1], [0, -1, 0, 1], [0, 1, -1, -1], [0, 1, -1, 1],
[0, 1, -1, 0], [0, 1, 1, -1], [0, 1, 1, 1], [0, 1, 1, 0], [0, 1, 0, -1],
[0, 1, 0, 1], [1, -1, -1, -1], [1, -1, -1, 1], [1, -1, -1, 0],
[1, -1, 1, -1], [1, -1, 1, 1], [1, -1, 1, 0], [1, -1, 0, -1],
[1, -1, 0, 1], [1, 1, -1, -1], [1, 1, -1, 1], [1, 1, -1, 0],
[1, 1, 1, -1], [1, 1, 1, 1], [1, 1, 1, 0], [1, 1, 0, -1], [1, 1, 0, 1],
[1, 0, -1, -1], [1, 0, -1, 1], [1, 0, -1, 0], [1, 0, 1, -1], [1, 0, 1, 1],
[1, 0, 1, 0]]