Final-1
Final-1
Final Exam
Section 15
• The duration of this exam is 3 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 135 points.
• You are not allowed to use the internet or previously stored material.
• Active cell phones and any other unauthorized electronic devices are absolutely not
allowed in the exam rooms. They should be turned off and put away.
• 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 (75 points): Problem 2, Problem 3, Problem 4.a
– Level 2 (35 points): Problem 4.b, Problem 5
• Detailed comments are worth partial credit.
• Read through all of them first and start them in the order that allows you to make
the most progress.
• Plan your time wisely. Do not spend too much time on any one problem.
• Good luck!
NAME:
LAU ID#:
1
Problem 1: Relax with Collatz (25 points)
Write a Python program which asks the user to enter an integer n. Then, your program
should print n/2 if n is even and 3n + 1 if n is odd. You may assume that the user will
always input an integer.
Example 1:
2
Problem 2: Mode Finder (25 points)
In statistics, the mode m of a set of data values is the value that appears most often in the
set. For instance, in the set of the following values:
• 1, 2, 3, 0, 1, 1, 2, 8, 3, 2, 0: there are two modes, which are 1 and 2 since they both
appear 3 times.
Write a Python function getMode(L) that takes a list of real numbers L as input and returns
the mode of the list L.
Any correct solution is worth 20/25 points. To get full grade, aim for O(n) expected
time complexity, where n =len(L).
Hint #1 : Use a dictionary to achieve the best efficiency.
Hint #2 : If you feel stuck, you may use the list.count() method to get you started.
Example 1:
3
Problem 3: Smart Cart (30 points)
In a grocery store, items are placed on counters and each item has a name and a price.
When customers enter the store to purchase things, they use a ‘smart’ shopping cart inside
of which they place their desired items. The shopping cart can identify each item placed
inside of it and displays the total amount to pay to the customer.
In this exercise, your task is to implement the logic that governs the shopping cart.
Specifically, you will need to create two classes: StoreItem and ShoppingCart.
a) StoreItem Class. A StoreItem object has two data attributes: name and price.
Implement the following method attributes for this class:
– init (self, n, p), which sets the name to the string n and price to the
real number p. Write the necessary assertions to make sure the inputs to the
constructor are of appropriate data types.
– str (self), which casts the StoreItem into a string of the following format:
Name: n, price: p.
Output:
– init (self), which takes no arguments and sets the data attribute self.cart
= {} (an empty dictionary).
– add item(self, item), which checks if type(item)==StoreItem and then in-
serts item into the dictionary self.cart.
4
– get total(self), which loops over all the elements of self.cart and returns
the total price to be paid by the customer.
– str (self), which allows one to print all the items inside the cart (including
the quantity) and the total price to be paid.
Test your code by executing the following:
print ( cart )
Output:
5
Problem 4: Square Test (30 points)
a) Square Test. Write a Python script which asks the user to enter an integer n, and
checks whether or not n is a square of another integer. That is, your program should
check whether or not there exists an integer x such that n = x2 .
If the answer is YES, your program is supposed to print x as shown below.
Example 1:
Example 2:
Example 3:
b) Faster test using the bisection method. To see why faster tests are needed, try
your solution of Part (a) on the integer
705346989381573741216180447274035273700409226922028309041200370362778813583574934801 =
8398493849384982083498209484893849389438492 .
It won’t stop! In this part, you are asked to implement a much faster Square Test
using ideas from the bisection method covered in class. For instance, it should run in
a split second on the above integer. Hints:
6
Problem 5: Check Inclusion (25 points)
Write a function checkInclusion(a,b) that, given two strings a and b, returns True if
b contains a permutation of a, or returns False otherwise. In other words, the function
should check if b contains an anagram of a. The table below contains a number of examples
of what the function is expected to return:
a b Output Reason
“live” “alive” True “alive”
“alive” “live” False len(a)>len(b)
“listen” “aiejsilentoaxr” True “aiejsilentoaxr”
“listen” “silen” False len(a)>len(b)
“listen” “aiejslnoaxr” False b does not contain anagram(s) of a
“civil” “lcviiiokeidl” True “lcviiiokeidl”
“civil” “dcxiiiokeidl” False b does not contain anagram(s) of a
“cece” “xvicaceec” True “xvicaceec”
“123abba1” “123abba1” True “123abba1”
Hint: It is very useful to define the isAnagram(s1, s2) function, which checks if s2 is
an anagram of s1.