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

Quiz 2

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)
17 views4 pages

Quiz 2

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 230 Introduction to Computation and Programming,

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

This a test txt


For problem 1

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.

Problem 2 (35 points). Exclusive OR of two sorted lists


Implement the function XOR(A,B), which given two lists A and B of integers sorted in strictly increasing
order, returns the a list C of integers consisting of all integers in A and not B or in B and not in A. The
list C is called the Exclusive OR (XOR) of A and B. Assume that all integers in A are distinct and all
integers in B are distinct. The order of elements in C doesn’t matter; any order is acceptable.
For instance, if A=[2, 5,10,11, 30] and B=[1, 2,3,5, 7,30], the function should return C=[10,11,1,3,7],
or any ordering of these elements. In particular, 2, 5, and 30 are excluded from C because they appear
in both A and B. All other elements are included because they appear in A and not in B, namely, 10
and 11, or in B and not in A, namely, 1, 3, and 7.
The use of the builtin types dict and set (if you are familiar with them) is strictly forbidden in this
problem.
Your function is not expected to work properly if the elements in A or B are not sorted in strictly
increasing order.
See also the examples in the below test program.
Any correct solution is worth 20/35 points. Faster solutions are worth more points. If you do in
log-linear time, you will get 30/35 points. To get full grade do it in linear time.
Submit your solution in a file called Prob2.py including your name and AUB user name as comments.
If you are not confident that your work on the efficient solution is fully correct, you are encouraged to
submit two files Prob2Slow.py and Prob2Fast.py containing the slow and efficient solutions, respectively.
If you would like to submit both the log-linear and the linear time solutions for grading, submit them as
Prob2FastLogLinear.py and Prob2FastLinear.py, respectively. As explained on Page 1, do not submit
commented code.
Test program/output:

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([], [])) []

Problem 3 (35 points). Find special index


In this problem, we are given a list consisting of integers sorted in strictly increasing order. We are
interested checking if the list has an element whose value is equal to its index. In particular, implement
the function specialIndex(L), which given a list L consisting of integers sorted in strictly increasing
order, returns an index i, where 0  i len(L) 1, such that L[i] = i, if such an i exists. If there is not
such i, your function should return 1. If there are multiple indicies i such that L[i] = i, any one of
them is a valid answer.
Examples:
For L=[-5,-2,2,7,11,20], specialIndex should return 2 since L[2] = 2.

For L=[-5,-2,3,7,11,20], specialIndex should return 1 since L[i] 6= i, for i = 0, . . . , 5.


For L=[-5,1,2,30], specialIndex should return 1 or 2 (both are valid answers) since since L[1] = 1
and L[2] = 2.
See also the examples in the below test program.
You are not asked to check if L is sorted in strictly increasing order, and you are not supposed to give
any guarantee on the behavior of your function if this is not the case.
Any correct solution is worth 15/35 points. Faster solutions are worth more points. To get full grade,
do it in O(log n) time.
Submit your code in a file called Prob3.py including your name and AUB user name as comments.
If you are not confident that your work on the efficient solution is fully correct, you are encouraged to
submit two files Prob3Slow.py and Prob3Fast.py containing the slow and efficient solutions, respectively.
As explained on Page 1, do not submit commented code.
Test program/output:
print(specialIndex([-5,-2, 2,7,11,20])) 2
print(specialIndex([ 0,1,3,7,11,20])) 0
print(specialIndex([-5,0,1, 3,10])) 3
print(specialIndex([-5,-2,3,7,11,20])) -1
print(specialIndex([-5, 1, 2,30])) 1
print(specialIndex([ 0])) 0
print(specialIndex([])) -1

Problem 4 (50 points). Recursion

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:

– For L = [6,5,1,4,1,5,5] and x = 5, count should return 3 since 5 appears 3 times in L.


– For L = [6,5,1,4,1,5,5] and x = 2, count should return 0 since 2 is not present in L.

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.

Move Disk 1 from Needle 1 to Needle 3


Move Disk 2 from Needle 1 to Needle 4
Move Disk 3 from Neelde 1 to Needle 2
Move Disk 2 from Needle 4 to Needle 2
Move Disk 1 from Needle 3 to Needle 2
Move Disk 4 from Needle 1 to Needle 4
Move Disk 5 from Neelde 1 to Needle 3
Move Disk 4 from Needle 4 to Needle 3
Move Disk 1 from Needle 2 to Needle 1
Move Disk 2 from Needle 2 to Needle 4
Move Disk 3 from Neelde 2 to Needle 3
Move Disk 2 from Needle 4 to Needle 3
Move Disk 1 from Needle 1 to Needle 3

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.

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