CS1010E PE2 2019+v3
CS1010E PE2 2019+v3
Grading Guidelines:
● No mark will be given if your code cannot run, namely any syntax error or crashes.
§ Your tutors will just grade what you have submitted and they will not fix your code.
§ Comment out any part that you do not want.
● Workable code that can produce correct answers will only give you partial marks. Only good and efficient code
will give you full marks. Marks will be deducted if your code is unnecessarily long, hard-coded, in poor
programming style, including irrelevant code or test code.
● You must use the same function names as those in the skeleton given.
● You cannot import any additional packages or functions other than those imported in the skeleton code
attached in this pdf file.
● Your code should be efficient and able to complete each example function call in this paper within 10 seconds.
● In all parts, you should return values instead of printing your output.
● You should remove all your test cases before submitting to Examplify. If you submit more code than required, it
may have a possibility of penalty because you code is “unnecessarily long”.
● You should save an exact copy of your submission in order to submit in Coursemology again after the PE. Any
difference between exemplify and coursemology submissions will be severely penalized.
1, 2, 3, 6, 12, …
The number 6 is the sum of all terms 1, 2 and 3, and 12 is the sum of all the terms 1, 2, 3 and 6.
We assume the first term is always 1. However, we have the freedom to change the second term. By setting different
second term, we got different sequence
1. You do not have to worry about the recursion depth that is greater than 994.
2. Usually the number and sequence are long, you can and you should verify by the last few digits of the last few
numbers only. If you print out the whole list or whole number, your Python IDLE console may be frozen, or even
your computer may.
The following sample outputs show that you may not want to print out everything to verify the correctness of your
output when the sequences and numbers are long.
>>> longSFS = superFibonacciSeqI(20,10**4321)
>>> len(longSFS)
14352
>>> from math import log
>>> int(log(longSFS[-1])/log(10))
4320
>>> longSFS[-1] % (10**10)
4087703552
Your function should be efficient and should complete each of the above function calls of superFibonacciSeqI()
in less than 5 seconds. You can use log for your own testing but not for submission.
Task 3 Search for the Smallest Second Term in SFS (10 marks)
With different second term, the SFS will contain different set of numbers. Given a number n, we want to find the
smallest second term that will generate an SFS that contains n. The above example in Task 1 showed that if the second
term is 10, the number n = 44 will be in the SFS. However, the number 44 is NOT in the SFS with 20 as the second term.
Write a function smallestSecondTermSFScontains(n) to search for the smallest second term that will
generate an SFS that contains the number n for n>1. Example:
>>> smallestSecondTermSFScontains(2016)
62
>>> smallestSecondTermSFScontains(9876)
2468
>>> smallestSecondTermSFScontains(23592960)
44
>>> smallestSecondTermSFScontains(2651336998912)
9876
To verify the above answers:
>>> superFibonacciSeqI(2468,10**7)
[1, 2468, 2469, 4938, 9876, 19752, 39504, 79008, 158016, 316032, 632064,
1264128, 2528256, 5056512]
Part 2 Pizza Delivery 2.0 (45 marks)
Yes, you did not read it wrongly, this task is similar to Assignment 6 and you can fully reuse your code there. However,
you will build some new extra features on this part and we will only reward you for the new features excluding what you
have done for the assignment. If you are not familiar with Assignment 6, we have attached that assignment at the end.
For the original assignment, pizza shops only have coordinates. We would like to enrich the pizza shop property here to
include the following attributes:
• r,c : Same as Assignment 6, the number of row and column of the map
• allPS: a list of instances of class PizzaShop
• currentHour: The current time in hour defined in the same way as the starthour and endhour of class
PizzaShop.
• A pizza shop will NOT deliver to any house that has a distance more than the radius. More preciously, it WILL
still deliver if the distance is EXACTLY the same as the radius.
• A pizza shop will only deliver when it is open. If the currentHour is not within its opening hours, the map
should NOT contain that pizza shop.
• On the map, the nearest pizza shop is shown by the first character of its name, instead of numbers. Same as
Assignment 6, if a house has more than two nearest pizza shops, then an ‘X’ is shown instead. We can assume
that no two pizza shops have the same first character in their names.
• And for any house that is not reachable by any pizza shop, just put a period ‘.’ in the map.
With r = 10 and c = 12, the three maps for three different values of currentHour as 10, 12 and 16 will be:
...A........ ...A........ ............
.AAAAA...... .AAAAA...... ............
.AAAAA...... .AAAAAB..... ......B.....
AAAAAAA..... AAAAAAXBB... ....BBBBB...
.AAAAA...... .AAAAXBBBB.. ...BBBBBBB..
.AAAAA...... .AAAXBBBBB.. ...BBBBBBB..
...A........ ..BXBBBBBBB. ..BBBBBBBBB.
............ ...BBBBBBB.. ...BBBBBBB..
............ ...BBBBBBB.. ...BBBBBBB..
............ ....BBBBB... ....BBBBB...
The red color of the ‘X’ is only to make it easier for you to read. You do not have to produce the color. More maps of
larger sizes are available at the end of this pdf.
However, if we are allowed to put any numbers of ‘+’ or ‘-’ signs between the digits on the left side of the equation, you
may have some correct equations. In fact, there are altogether 11 ways of setting the above equation right:
1+2+3-4+5+6+78+9=100
1+2+34-5+67-8+9 =100
1+23-4+5+6+78-9 =100
1+23-4+56+7+8+9 =100
12+3+4+5-6-7+89 =100
12+3-4+5+67+8+9 =100
12-3-4+5-6+7+89 =100
123+4-5+67-89 =100
123+45-67+8-9 =100
123-4-5-6-7+8-9 =100
123-45-67+89 =100
The above equation is just one example, and its left side can change from ‘123456789’ to any set of digits, and its right
side can be any number. E.g. if the left side digits are ‘111111’ and the right number is 121, we have:
11+111-1=121
11-1+111=121
111+11-1=121
111-1+11=121
Moreover, you can also extend the set of operators from the two signs ‘+’ and ‘-’ to more variety of operators, e.g. ‘+-
*%’ with the initial equation ‘12345=30’, we have two correct equations:
1+2*3*4+5=30
1%2+34-5 =30
Task 6 (25 marks)
Write a function sumTo(leftdigits,ops,n) to return a list of all correct equations in strings, with
• leftdigits: A string containing the digits on the left. We will not include the digit zero.
• ops: A string of all possible operators. We will limit the possible operators to +, -, * and % only.
• n: The number on the right of the equation represented as an integer
On the left side, you can put at most one operator (or none) between any pair of digits. The minus sign will be treated as
subtraction, not the unary negative operator.
Your output should be in ascending lexicographic order, and you are allowed to use the built-in sorting functions.
Hints
This problem seems to be difficult, but here are some hints that maybe useful to you.
First, the built-in function eval() should be of great help. The simple usage of this function is to take in a string and
evaluate the value for you. For example:
>>> x = 2
>>> eval('x*3')
6
Second, you may write the following functions to help the task.
More hints: The answer for product(str1,n) is to add each of the character in str1 to product(str1,n-1).
For example, product(‘ab’,3) is adding ‘a’ to all items in product(‘ab’,2) and adding ‘b’ to all items in
product(‘ab’,2’).
There is a more complicated version of this function product() in the package itertools, but you are not allowed
to import that (or other functions).
print(superFibonacciSeqR(4,10))
#ans: [1, 4, 5, 10, 20, 40, 80, 160, 320, 640]
sfs1 = superFibonacciSeqR(11,20)
print(sfs1[-3::])
#ans: [393216, 786432, 1572864]
print(superFibonacciSeqI(4,10))
#ans: [1, 4, 5, 10]
#longone = superFibonacciSeqI(20,10**9876)
#print(longone[-1]%10000000)
#ans: 6179584
#len(longone) should be 32805
def smallestSecondTermSFScontains(n):
return 1
print(smallestSecondTermSFScontains(2016))
print(smallestSecondTermSFScontains(9876))
print(smallestSecondTermSFScontains(2651336998912))
print(smallestSecondTermSFScontains(23592960))
#ans: 62, 2468, 9876, 44
################################################################
#Part 2
class PizzaShop:
def __init__(self,pos,name,radius,starthour,endhour):
self.pos = pos
self.name = name
self.radius = radius
self.starthour = starthour
self.endhour = endhour
def createZeroMatrix(n,m):
return [[0 for i in range(m)] for j in range(n)]
def mTightPrint(m):
for i in range(len(m)):
line = ''
for j in range(len(m[0])):
line += str(m[i][j])
print(line)
def PDMap(r,c,allPS,currentHour):
return [[]]
allPS2 = []
allPS2.append(PizzaShop([20,10],'Amazing Pizza',10,8,22))
allPS2.append(PizzaShop([29,30],'Beloved Pizza',10,8,22))
#mTightPrint(PDMap(50,60,allPS2,10))
allPS3 = []
allPS3.append(PizzaShop([20,30],'Amazing Pizza',10,8,22))
allPS3.append(PizzaShop([38,30],'Beloved Pizza',10,8,22))
#mTightPrint(PDMap(50,60,allPS3,10))
allPS = []
allPS.append(PizzaShop([20,10],'Amazing Pizza',12,8,22))
allPS.append(PizzaShop([29,30],'Beloved Pizza',17,8,22))
allPS.append(PizzaShop([41,20],'Cute Pizza',16,14,22))
allPS.append(PizzaShop([45,55],'Delicious Pizza',12,8,22))
allPS.append(PizzaShop([10,58],'Elegant Pizza',12,8,22))
allPS.append(PizzaShop([35,68],'Fancinating Pizza',12,14,22))
allPS.append(PizzaShop([32,60],'Good Pizza',15,8,22))
allPS.append(PizzaShop([30,46],'Ideal Pizza',9,8,14))
#mTightPrint(PDMap(50,80,allPS,10))
#mTightPrint(PDMap(50,80,allPS,14))
allPSsmall = []
allPSsmall.append(PizzaShop([3,3],'Amazing Pizza',3,8,14))
allPSsmall.append(PizzaShop([6,6],'Bear Pizza',4,12,22))
#mTightPrint(PDMap(10,12,allPSsmall,10))
#mTightPrint(PDMap(10,12,allPSsmall,16))
#mTightPrint(PDMap(10,12,allPSsmall,12))
################################################################
# Part 3
def sumTo(str1,ops,n):
return[]
(0,0) (2,5)
#rows = h
#columns = w
A new brand of pizza came to town! They set up n stores in some of the buildings in town for some n £ 10. We store the
coordinates of the pizza stores in a list. For example, a list of
[[10,20],[30,20],[40,50]]
represents three stores of pizza with Store 0 at (10,20), Store 1 at (30,20) and Store 2 at (40,50)*. (Somehow the
big boss of the pizza stores knows programming and he starts counting by 0 also.) There is no two pizza stores at the
same location. All the people in Regulaville only eat pizza at their own home and call for delivery. And all the stores will
deliver pizza by flying drones that will fly directly from the stores to the destination.
In order to minimize the time and power used by the drones, the mayor ordered that every home must only order pizzas
from the nearest store, unless there are more than one store with equal minimal distance. For example, for the three
pizza stores mentioned about, the house at coordinates (40,20) will be closest to Store 1 with the nearest distance 10
km. Compared to Stores 0 and 2 with the same distance 30 > 10 km.
*The coordinates are a bit confusing because for a location [10,20] in our assignment, it means going south (vertical
direction) for 10km and 20km east (horizontal direction), in which, it’s the opposite way we think in real life such that
(x,y) in which x is the horizontal direction and y is the vertical one.
Task
Write a function PDMap(h,w,pizzaLoc) to compute a map for ALL houses in Regulaville to show the closest pizza
store number to each house. As mentioned above, each house has the coordinates (i,j) such that 0 £ i < h and 0
£ j < w and the list pizzaLoc is a list of pizza store coordinates. You can assume the number of pizza stores is less
than or equal to 10 and their numbers are ranging from 0 to 9. Here is a sample usage of the function PDMap().
For example, it shows that the home at (3,6) is closest to the pizza Store 1.
Sometime, there is a chance that some of the house has more than one pizza store that are closest to it with the same
minimal distance. If it happens, mark that house with an ‘X’. Since we are all using integer arithmetic, the distance
computation must be exact. (Wait, isn’t that a “sqrt()” in the distance computation?)
>>> mTightPrint(PDMap(10,10,[[2,3],[4,9],[7,2]]))
0000000X11
0000000111
0000000111
000000X111
X000001111
22222X1111
2222221111
2222222111
2222222111
2222222X11
In this part, you should submit at least the function PDMap(), or together with any functions you created to support
your function.