Quiz 2
Quiz 2
Spring 2023-23
Quiz II
April 1, 2023
The duration of this exam is 2 hours. Keep in mind that you need around 5 minutes at the end of
the duration of the exam to submit your answers.
The exam consists of 4 problems for 145 points.
The exam will be held without internet access on the lab machines as detailed below
– All exam machines will be logged in to temporary accounts and disconnected from the network
before you enter the rooms
– On these machines, you will find on Drive D two folders:
EECE230Quiz2Material: This folder has all material included in Quiz 2, namely slides
and their associated code, assignments and their solutions, as well as solving sessions and
their solutions
EECE230Quiz2SubmissionFolder: This is an empty folder in which you are ex-
pected to put your code after renaming the folder with your AUB user name. For in-
stance, you are expected to rename this folder as “abc123” if your AUB email is
“abc123@mail.aub.edu”. This folder will be collected as your exam submission at the
end of the exam. Please do not compress this folder and keep in mind that any code
you put outside this folder won’t be collected for grading. 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.
Cell phones and any other unauthorized electronic devices are absolutely not allowed in the exam
rooms. They should be turned o↵ and put away.
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 and you will be referred to MSFEA disciplinary
committee.
You are asked to include your name and AUB user name as comments in reach file for
identification purposes, but please keep in mind that all comments and docstrings will removed
from your files to anonymize the submissions before grading. Accordingly, any code included in
your submission as comments won’t be visible during the grading process.
The problems are of varying difficulty. Below is a rough ordering estimate of the problems in order
of increasing difficulty.
– Level 1 (60 points): Problems 1 and nonefficient solutions of Problems 2 and 3
– Level 1-2 (60 points): Part 4.a and efficient solutions of Problems 2 and Problems 3
– Level 2-3 (25 points) Part 4.b
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). Copy file
Implement the function copyFile(inFileName,outFileName), which given two strings inFileName and
outFileName, where inFileName is the name of an existing input file, copies the content of the input file
into a new file whose name is outFileName.
For instance if inFileName = "File1.txt", outFileName = "File2.txt", and the content of “File1.txt”
is
abc
your function should create a new file whose name is "File2.txt" and whose content is exactly as that
of "File1.txt", i.e.,
This a test txt
For problem 1
abc
If inFileName is the same as outFileName, your function should raise the exception "Input and output
files are the same file!".
Any correct solution is worth full grade.
Submit your solution in a file called Prob1.py including your name and AUB user name as comments.
2
print(XOR([2, 5, 1000], [30,50,300])) [2, 5, 1000, 30, 50, 300]
print(XOR([20, 50, 100], [3, 4])) [20, 50, 100, 3, 4]
print(XOR([2,5,10, 11, 30], [1,2,3,5,7,30])) [10, 11, 1, 3, 7]
print(XOR([2, 5, 10, 30, 200], [1, 2, 3,5,30])) [10, 200, 1, 3]
print(XOR([2, 5, 10], [2, 5,10])) []
print(XOR([], [3])) [3]
print(XOR([3], [])) [3]
print(XOR([], [])) []
a) (25 points) Count occurences. Implement the function count(L,x), which given a list L of
integers and an integer x, returns the number of occurences of x in L.
The use of for or while loops is strictly forbidden in this part. Also, the use of the in
operator and all methods associated with the list type is strictly forbidden. You are asked
to solve it using recursion. Namely, implement count as a wrapper function which calls a recursive
function countRecursive with appropriate input parameters.
Examples:
3
See also the examples in the below test program.
Any correct solution is worth full grade.
Submit your code in a file called Prob4a.py including your name and AUB user name as comments.
Test program:
L = [6,1,4,1,5,5,6,1,1,7,2]
for x in range(9):
print("count("+str(L)+","+str(x)+"):", count(L,x))
Output:
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],0): 0
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],1): 4
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],2): 1
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],3): 0
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],4): 1
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],5): 2
count([ 6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],6): 2
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],7): 1
count([6, 1, 4, 1, 5, 5, 6, 1, 1, 7, 2],8): 0
b) (25 points) Hanoi with special extra needle. Consider the following variation of the tower of
Hanoi problem. As in the original puzzle, we have n disks on Needle 1 which we would like to move
to Needle 3 using Needle 2 as an intermediate needle. The di↵erence is that we have the special
extra needle Needle 4, which can use also an intermediate needle but we are not allowed to put
on Needle 4 more than one disk at time. That is, we would like to move n disks from Needle 1 to
Needle 3 using Needles 2 and 4 as intermediate needle subject to the following rules:
(i) Only one disk can be moved at a time
(ii) The removed disk must be placed on one of the needles
(iii) A larger disk cannot be placed on top of a smaller disk.
(iv) Needle 4 can handle at most one disk
Recall that in the original puzzle, the number of moves needed to move n disks is 2n 1. We would
like to take advantage of Needle 4 to move the disks with less moves.
For instance, for n = 5, we need 25 1 = 32 moves in the original puzzle. However, we can take
advantage of the special extra needle to do it with 13 moves only as shown below. Note that the
solution is not unique. That is, you may end up with an other sequence of moves.
Any solution which moves the n disks in o(2n ) moves (little o of 2n ) is worth full grade. Call your
function moveDisksExtra.
(Hint: Recursion)
Submit your code in a file called Prob4b.py including your name and AUB user name as comments.