0% found this document useful (0 votes)
11 views4 pages

Final Openenrollement S22 230

Uploaded by

karimabboud05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Final Openenrollement S22 230

Uploaded by

karimabboud05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EECE 230X – Introduction to Computation and Programming

Open Enrollment Section


Second Final Exam

July 16, 2022

ˆ 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

Submit your solution in a file called Prob1.py.

Problem 2 (40 points). Time Interval class


In this problem, you will design an abstract data type for a time interval. An interval is represented
by two real numbers start and end, representing the interval’s start time and the end time, respectively.
We assume that start ≤ end. Design the class timeInterval which defines a time interval as an abstract
data type. Include the data attributes start and end and the method attributes:
ˆ init , which takes start and end as input arguments with zero default values. This method
should return the exception "Incorrect type!" if the types of start or end are not float or int. It
should also return the exception "Invalid interval!" if start>end.
ˆ Special method str , which casts the point into a string of the form "[start,end]".

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

Include also the following function (not a method of timeInterval):


ˆ disjoint(I,J), which given two timeIntervals I and J, returns True if I and J are disjoint and false
otherwise. This method should return the exception "Incorrect type!" if the type of I or J is not
timeInterval.

Any correct solution is worth full grade.


Test program: Output:

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

Submit your solution in a file called Prob2.py.

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

ii) the entries of each column of M are in increasing order.


Otherwise, the functions returns False.

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

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
i = 1
for M in (M1,M2,M3,M4):
print("M"+str(i)+":")
print(np.matrix(M))
print(increasingRowsAndCols(M),"\n")
i+=1

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

Submit your solution in a file called Prob3.py.

Problem 4 (40 points). Generate lists


Write a function genLists(n), which given integer n, returns a list consisting of all length-n lists L
such that:
ˆ The entries of L are integers in the range −1, 0, 1

3
ˆ L does not have contiguous zeros, i.e., if L[i] = 0 then L[i + 1]! = 0, for i = 0, . . . , n − 2.

See the below test program.


(Hint: Recursion.)
Faster algorithms are worth more points. Any correct solution is worth 20 points.
Test program:

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

Submit your solution in a file called Prob4.py.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy