Uploaded by nadimchibani

quiz2

advertisement
EECE 230 Introduction to Computation and Programming,
Section 1
Quiz II
Nov 21, 2020
ˆ The duration of this exam is 2 hours. Keep in mind that you need around 10 minutes at the end of
the duration of the exam to submit your answers. It is your responsibility to make sure your files
are correctly submitted.
ˆ The exam consists of 5 problems for 150 points
ˆ You can use all the material available on Moodle.
ˆ You are asked to submit a single zip file containing your Python files (ending with .py extension).
Failure to do so may lead to a failing grade on the exam. It is your responsibility to make sure your
files are correctly submitted.
ˆ You are only allowed to use the internet to access Moodle
ˆ You are not allowed to use previously stored materials on your machine.
ˆ If you get caught violating the above rules or if you communicate with a person other than the exam
proctors during the exam, you will immediately get zero and you will be referred to the appropriate
disciplinary committee.
ˆ The problems are of varying difficulty. Below is a rough ordering estimate of the problems in order
of increasing difficulty.
– Level 0 (25 points): Problem 1
– Level 1-2 (100 points): Problems 2, 3, 4, and 5.a
– Level 3 (25 points): Problem 5.b
ˆ 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 (25 points). Relax
Write a python script to plot the 4 lines as shown in the following figure:
Any correct solution is worth full grade.
Submit your solution in a file called Prob1.py including your name and ID number.
Problem 2 (25 points). Reverse list recursively
Implement the function reverseList(L), which given a list L, modifies L by reversing the order of
its elements. The use of for loops, while loops, slicing operator, list.reverse method, list.copy method,
and copy module is strictly forbidden in this problem. You are asked to do it recursively. Namely,
implement reverseList as wrapper function which calls a recursive function reverseListRecursive.
Any correct solution is worth full grade.
Test program/output:
L1 = []
L2 = [1]
L3 = [1, 2]
L4 = [10,5,3,2,1]
for L in (L1,L2,L3,L4):
print("L:",L)
reverseList(L)
print("L reversed:",L)
L: []
L reversed: []
L: [1]
L reversed: [1]
L: [1, 2]
L reversed: [2, 1]
L: [10, 5, 3, 2, 1]
L reversed: [1, 2, 3, 5, 10]
Submit your solution in a file called Prob2.py including your name and ID number.
Problem 3 (25 points). Check if intervals are disjoint
In this problems we are interested in time intervals. We represent a time interval as a tuple (s, f ),
where s is a float representing the start time and f is a float representing the finish time. We assume
that s ≤ f . Two intervals (s1 , f1 ) and (s2 , f2 ) are called disjoint if f1 < s2 or f2 < s1 . For instance, (0, 1)
and (2, 4) are disjoint because 1 < 2, but (1, 3) and (2, 4) are not disjoint.
Given a list L of tuples representing intervals, we would like to check whether the intervals in L are
disjoint, i.e., whether L[i] and L[j] are disjoint for all i = 0, . . . , n − 1 and j = 0, . . . , n − 1 such that
i 6= j, where n =len(L).
2
Implement the function areDisjoint(L), which given L, checks whether or not the intervals in L are
disjoint. If they are disjoint, your function should return True. Otherwise, it should return False.
For instance, for L = [(11,125),(1,1.9), (100,150),(3,5)], the answer is False since (11, 125) and
(100, 150) are not disjoint.
Any correct solution is worth 15/25 points. Faster solutions are worth more points. To get full grade
do it in O(n log n) time.
Test program/output:
True
False
False
True
True
False
False
L1 = [(1,3)]
L2 = [(2,4),(1,3)]
L3 = [(2,5),(3,4)]
L4 = [(2,4),(0,1)]
L5 = [(11,25),(1,1.9), (100,150),(2,5)]
L6 = [(11,25),(1,2), (100,150),(2,5)]
L7 = [(11,125),(1,1.9), (100,150),(3,5)]
for L in (L1,L2,L3,L4,L5,L6,L7):
print(areDisjoint(L))
Submit your solution in a file called Prob3.py including your name and ID number.
Problem 4 (25 points). Remove duplicated strings
Implement the function removeDuplicatedStrings(A), which given a lists A of strings, returns a list B
of strings consisting of the strings in A without duplicated strings. That is, each string in A must appear
in B exactly once and each string in B must be a string in A.
Any correct solution is worth 15/25 points. Faster solutions are worth more points. To get full grade
do it in expected linear time.
Test program/output:
A1=[’ab’]
A2=[’ab’,’ab’]
A3=[’ab’,’abc’,’ab’]
A4=[’ab’,’xyx’,’ab’,’xyx’]
A5=[’xyx’,’ab’,’xy’,’ab’,’xyx’]
A6=[’xyx’,’bc’,’xyx’,’ab’,’xyx’]
for A in (A1,A2,A3,A4,A5,A6):
print(removeDuplicatedStrings(A))
[’ab’]
[’ab’]
[’ab’, ’abc’]
[’ab’, ’xyx’]
[’xyx’, ’ab’]
[’xyx’, ’bc’, ’ab’]
Submit your code in a file called Prob4.py including your name and ID number.
Problem 5 (50 points). Generate pattern matrix
a) (25 points) Concatenating matrices horizontally. In this part, we are given two matrices A
and B represented as lists of lists. Assume that the number of columns of A is equal to that B and
that the number of rows of A is equal to that B.
We can concatenate A and B vertically using the list + operators. For instance, consider the 2 × 3
matrices A = [[1, 2, 3],[4, 5, 6]] and B = [[7, 8, 9 ], [10, 11, 12]]. Then A+B is the 4 × 3
matrix [[1, 2, 3],[4, 5, 6], [7, 8, 9 ], [10, 11, 12]].
You are asked in this part to write a function to concatenate them horizontally.
For instance, the horizontal concatenation of the above matrices A and B is the 2 × 6 matrix
[[1, 2, 3, 7, 8, 9 ],[4, 5, 6, 10, 11, 12]].
Implement the function concatenateHorizontal(A,B), which given two matrices A and B represented
as lists of lists, returns their horizontal concatenation. Assume that the number of rows of A is
equal to the number of rows of B. Your function is not supposed to check if this condition holds.
Any correct solution is worth full grade.
3
Test program/output:
import numpy
A = [[1, 2, 3],[4, 5, 6]]
B = [[7, 8, 9 ], [10, 11, 12]]
C = A+B
D = concatenateHorizontal(A,B)
print("A:")
print(numpy.matrix(A))
print("\nB:")
print(numpy.matrix(B))
print("\nC:")
print(numpy.matrix(C))
print("\nD:")
print(numpy.matrix(D))
A:
[[1 2 3]
[4 5 6]]
B:
[[ 7 8 9]
[10 11 12]]
C:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D:
[[ 1
[ 4
2
5
3 7 8 9]
6 10 11 12]]
b) (25 points) Generate pattern. Implement the function generatePatern(k), which given an
integer k ≥ 0, returns a 2k × 2k matrix as shown in the examples below. Guess the pattern.
(Hint: Use recursion and Part (a)).
Any correct solution is worth full grade.
Test program:
import numpy
for k in range(5):
print("k=",k,":")
print(numpy.matrix(generatePattern(k)),"\n")
# For fun: show matrix as a figure with 1 and
import matplotlib.pyplot as plt
plt.imshow(generatePattern(10))
plt.show()
Output:
k= 0 :
[[1]]
k= 1 :
[[1 1]
[0 1]]
k= 2 :
[[1 1 1
[0 1 0
[0 0 1
[0 0 0
1]
1]
1]
1]]
k= 3 :
[[1 1 1
[0 1 0
[0 0 1
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
1
1
1
1
0
0
0
0
1
0
0
0
1
0
0
0
1
1
0
0
1
1
0
0
1
0
1
0
1
0
1
0
1]
1]
1]
1]
1]
1]
1]
1]]
4
0 in different colors
k= 4 :
[[1 1 1
[0 1 0
[0 0 1
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
[0 0 0
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
1
0
1
0
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
0
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
0
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]
1]]
Submit your code in a file called Prob5.py including your name and ID number.
5
Download