Prev Year PE2
Prev Year PE2
Instructions:
• Your code will be graded and run on Python 3.10.
• You cannot import any packages or functions unless some questions specifically allow you to do so. You also
cannot use any decorators. However, you are allowed to write extra functions to structure your code better
(except Part 1). Do remember to copy them over to the specific part(s) they are supporting.
• No marks will be given if your code cannot run, namely if it causes any syntax errors or program crashes.
o We will just grade what you have submitted and we will not fix your code.
o Comment out any lines of code that you do not want us to grade.
• Working code that can produce correct answers in public test cases will only give you partial marks. Your code
must be good and efficient and pass ALL public and hidden test cases for you to obtain full marks. Marks will be
deducted if it is unnecessarily long, hard-coded, in a poor programming style, or includes irrelevant code.
• You should either delete all test cases or comment them out (e.g. by adding # before each line) in your code.
Submitting more (uncommented) code than needed will result in a penalty as your code is "unnecessarily long".
• The code for each part should be submitted separately. Each part is also "independent': if you want to reuse
some function(s) from another part, you will need to copy them (and their supporting functions, if any) into the
part you are working on. However, any redundant functions for the part (albeit from a different part) will be
deemed as "irrelevant code".
• You must use the same function name as specified in the question. You must also use the same function
signature and input parameters without adding any new parameters.
• You are not allowed to mutate the input arguments.
• In general, you should return instead of print your output, unless you are specifically told to do so.
• Reminder: Any kind of plagiarism such as copying code with modifications will be caught. This includes adding
comments, changing variable names, changing the order of lines/functions, adding useless statements, etc.
• You are not allowed to use any generative AI tools such as ChatGPT in the assessment.
For example, the last number 149 = 24 + 44 + 81. Write a recursive function fib3_r(i) to return the ith Fibonacci
number of order 3. Your function should be purely recursive, which means you cannot use any loops or list
comprehension in this task. Here are some example output.
>>> print(fib3_r(5))
7
>>> print(fib3_r(11))
274
>>> print(fib3_r(20))
66012
You can see the number 857 is equal to 29+57+113+222+436. Write a recursive function fibn_r(i,n) to return the
ith Fibonacci number of order n. Your function should be purely recursive, which means you cannot use any loops or list
comprehension in this task. Here are some example output.
>>> print(fibn_r(13,5))
857
>>> print(fibn_r(21,4))
207529
>>> print(fibn_r(14,7))
705
For the original assignment, pizza shops only have coordinates. We would like to enrich the pizza shop property by
defining a class Pizza_shop with the following attributes (Please read the details in the given code template):
Wait, robots? What robots?! Sorry that there is a new law to ban all flying drones in Regulaville. And we have to deliver
with automated autonomous robots! (How cool is that!). However, the robots can only travel on the roads. And in
Regulaville, all roads are only going NS or EW directions. For example, from pizza shop at (2,5) to a house at (4,2), a
robot travels 5 km to deliver a pizza (see the diagram below). Each pizza shop has robots that can travel back and forth
to a house within a maximum distance of max_dist km only. (Namely, the robot actually can travel max_dist×2 to
return.) This type of distance measure is called Manhattan distance.
(2,5)
Distance = 5
(4,2)
• r,c : Same as Assignment 6, the number of row and column of the map
• shops: a list of instances of class Pizza_Shop
• A pizza shop will NOT deliver to any house that has a distance more than the max_dist. More precisely, it
WILL still deliver if the distance is EXACTLY the same as the max_dist.
• 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 pizza shop have ‘X’ as the first character in its name.
• And for any house that is not reachable by any pizza shop, just put a period ‘.’ in the map.
For example, if we initialize the list of pizza shop as follows:
shops = [Pizza_Shop(3,3,3,'Ace Pizza'),Pizza_Shop(6,6,5,'Bear Pizza')]
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.
Lastly, you should not modify the class Pizza_shop and you do not need to submit that class.
Part 3 NE Paths (15 + 15 = 30 marks)
Before we start, please be reminded that you are not allowed to use any global variables in this part. On the other hand,
we do not require that every task has to be independent in this part. Meaning, you could call the functions in Task 2 in
your Task 1 solution if you want. In this part, you will get full mark only if your function can handle large numbers of A
and B within 1 second.
Mr. Norwee is at the map position (0,0). And he wants to go the final destination with coordinates (A,B) for two
integers A,B >=0. However, Mr. Norwee has some very special habits:
In another word, the above rules mean that if Mr. Norwee is in a position (x,y), he can only move to either one of the
positions (x+1,y) or (x,y+1).
How many different paths are there for him to go from (0,0) to (A,B)?
For example, if A = 1 and B = 2, there are three possible ways for him, namely,
1. (0, 0) > (0, 1) > (0, 2) > (1, 2)
2. (0, 0) > (0, 1) > (1, 1) > (1, 2)
3. (0, 0) > (1, 0) > (1, 1) > (1, 2)
Another example, if A = 2 and B = 2, there are six possible ways for him, namely,
1. (0, 0) > (0, 1) > (0, 2) > (1, 2) > (2, 2)
2. (0, 0) > (0, 1) > (1, 1) > (1, 2) > (2, 2)
3. (0, 0) > (1, 0) > (1, 1) > (1, 2) > (2, 2)
4. (0, 0) > (0, 1) > (1, 1) > (2, 1) > (2, 2)
5. (0, 0) > (1, 0) > (1, 1) > (2, 1) > (2, 2)
6. (0, 0) > (1, 0) > (2, 0) > (2, 1) > (2, 2)
>>> print(n_ways(0,0))
1
>>> print(n_ways(1,2)) >>> print(n_ways(13,5))
3 8568
>>> print(n_ways(2,2)) >>> print(n_ways(100,20))
6 29462227291176635718126
>>> print(n_ways(11,3)) >>> print(n_ways(600,201) % 100000000)
364 80010800
Task 2 Compute NE Paths with Blocks
However, some of the junctions are blocked. Namely, there is NO way to travel through some junctions. For example, if
there are two blocks at (1,2) and (1,3), there will be only 14 ways to go from (0,0) to (3,4).
(3,4)
(0,0)
n_ways_with_blocks (3,4,((1,2),(1,3)) = 14
Write a function n_ways_with_blocks (A,B,blocklist) to return the number of ways that Mr. Norwee can
travel from (0,0) to (A,B), in which blocklist is a tuple of locations of blockage that each location is stored as a
tuple. You can assume that A,B >= 0. Here are some sample outputs:
################################################################
#Part 2
def pizza_map(r,c,shops):
return pass
def mtp(m):
for row in m:
print(''.join(map(str,row)))
allPS = []
allPS.append(Pizza_Shop(20,10,8,'Amazing Pizza'))
allPS.append(Pizza_Shop(29,31,13,'Beloved Pizza'))
allPS.append(Pizza_Shop(38,20,10,'Cute Pizza'))
allPS.append(Pizza_Shop(45,55,20,'Delicious Pizza'))
allPS.append(Pizza_Shop(10,58,5,'Elegant Pizza'))
allPS.append(Pizza_Shop(35,68,7,'Fancinating Pizza'))
allPS.append(Pizza_Shop(32,60,11,'Good Pizza'))
mtp(pizza_map(50,60,allPS)) # First map in the next next page
print()
allPS.append(Pizza_Shop(30,46,12,'Ideal Pizza'))
mtp(pizza_map(50,60,allPS)) # Second map in the next next page
################################################################
#Part 3
def n_ways(A, B):
pass
def n_ways_with_blocks(A, B, blocks):
pass
# You should not submit the following code. They are for your testing only
print(n_ways(1,2)) # ans: 3
print(n_ways(2,2)) # ans:6
print(n_ways(11,3)) # ans:364
print(n_ways(13,5)) # ans: 8568
print(n_ways(100,20)) # ans:29462227291176635718126
print(n_ways(600,201) % 100000000) # ans: 80010800
print(n_ways_with_blocks (0,0,())) # ans: 1
print(n_ways_with_blocks (0,0,((0,0),))) # ans: 0
block = ((2,3),(0,3),(6,9),(5,11))
print(n_ways_with_blocks (6,11,block)) # ans: 1131
block3 = block + ((1,4),(3,4),(4,5))
print(n_ways_with_blocks (6,11,block3)) # ans: 71
print(n_ways_with_blocks (40,50,block3)) # ans: 13775028282698978194313826
............................................................
............................................................
............................................................
............................................................
Example cases for Pizza Delivery
............................................................
..........................................................E.
.........................................................EEE
........................................................EEEE
.......................................................EEEEE
......................................................EEEEEE
.....................................................EEEEEEE
......................................................EEEEEE
..........A............................................EEEEE
.........AAA............................................EEEE
........AAAAA............................................EEE
.......AAAAAAA............................................E.
First map
......AAAAAAAAA................B............................
.....AAAAAAAAAAA..............BBB...........................
....AAAAAAAAAAAAA............BBBBB..........................
...AAAAAAAAAAAAAAA..........BBBBBBB.........................
..AAAAAAAAAAAAAAAAA........BBBBBBBBB........................
...AAAAAAAAAAAAAAA........BBBBBBBBBBB.......................
....AAAAAAAAAAAAA........BBBBBBBBBBBBB.....................G
.....AAAAAAAAAAA........BBBBBBBBBBBBBBB...................GG
......AAAAAAAAA........BBBBBBBBBBBBBBBBB.................GGG
.......AAAAAAA........BBBBBBBBBBBBBBBBBBB..............DGGGG
........AAAAA........BBBBBBBBBBBBBBBBBBBBB............DGGGGG
.........AAA........BBBBBBBBBBBBBBBBBBBBBBB..........DGGGGGG
..........A........BCBBBBBBBBBBBBBBBBBBBBBBB........DGGGGGGG
..................BCCXBBBBBBBBBBBBBBBBBBBBBBB......DGGGGGGGG
..................CCCCXBBBBBBBBBBBBBBBBBBBBB......DGGGGGGGGG
.................CCCCCCXBBBBBBBBBBBBBBBBBBB......DGGGGGGGGGG
................CCCCCCCCXBBBBBBBBBBBBBBBBB......DGGGGGGGGGGG
...............CCCCCCCCCCXBBBBBBBBBBBBBBB......DDDGGGGGGGGGG
..............CCCCCCCCCCCCXBBBBBBBBBBBBB......DDDDDGGGGGGGGG
.............CCCCCCCCCCCCCCXBBBBBBBBBBB......DDDDDDDGGGGGGGG
............CCCCCCCCCCCCCCCCXBBBBBBBBB......DDDDDDDDDXXXGGGG
...........CCCCCCCCCCCCCCCCCCXBBBBBBB......DDDDDDDDDDDDDXGGG
..........CCCCCCCCCCCCCCCCCCCCXBBBBB......DDDDDDDDDDDDDDDXGG
...........CCCCCCCCCCCCCCCCCCCBBBBB......DDDDDDDDDDDDDDDDDXG
............CCCCCCCCCCCCCCCCCBBBBB......DDDDDDDDDDDDDDDDDDDX
.............CCCCCCCCCCCCCCC..BBB......DDDDDDDDDDDDDDDDDDDDD
..............CCCCCCCCCCCCC....B......DDDDDDDDDDDDDDDDDDDDDD
...............CCCCCCCCCCC...........DDDDDDDDDDDDDDDDDDDDDDD
................CCCCCCCCC...........DDDDDDDDDDDDDDDDDDDDDDDD
.................CCCCCCC...........DDDDDDDDDDDDDDDDDDDDDDDDD
..................CCCCC.............DDDDDDDDDDDDDDDDDDDDDDDD
...................CCC...............DDDDDDDDDDDDDDDDDDDDDDD
....................C.................DDDDDDDDDDDDDDDDDDDDDD
.......................................DDDDDDDDDDDDDDDDDDDDD
............................................................
............................................................
............................................................
............................................................
............................................................
..........................................................E.
.........................................................EEE
........................................................EEEE
.......................................................EEEEE
......................................................EEEEEE
.....................................................EEEEEEE
......................................................EEEEEE
..........A............................................EEEEE
.........AAA............................................EEEE
........AAAAA............................................EEE
.......AAAAAAA............................................E.
......AAAAAAAAA................B............................
.....AAAAAAAAAAA..............BBB...........................
....AAAAAAAAAAAAA............BBBBB............I.............
...AAAAAAAAAAAAAAA..........BBBBBBB..........III............
..AAAAAAAAAAAAAAAAA........BBBBBBBBB........IIIII...........
...AAAAAAAAAAAAAAA........BBBBBBBBBBB......IIIIIII..........
....AAAAAAAAAAAAA........BBBBBBBBBBBBB....IIIIIIIII........G
.....AAAAAAAAAAA........BBBBBBBBBBBBBBB..IIIIIIIIIII......GG
......AAAAAAAAA........BBBBBBBBBBBBBBBBBIIIIIIIIIIIII....GGG
.......AAAAAAA........BBBBBBBBBBBBBBBBBXIIIIIIIIIIIIII.DGGGG
........AAAAA........BBBBBBBBBBBBBBBBBBXIIIIIIIIIIIIIIIGGGGG
.........AAA........BBBBBBBBBBBBBBBBBBBXIIIIIIIIIIIIIIXGGGGG
..........A........BCBBBBBBBBBBBBBBBBBBXIIIIIIIIIIIIIIXGGGGG
..................BCCXBBBBBBBBBBBBBBBBBXIIIIIIIIIIIIIIXGGGGG
..................CCCCXBBBBBBBBBBBBBBBXIIIIIIIIIIIIIIIXGGGGG
.................CCCCCCXBBBBBBBBBBBBBBXIIIIIIIIIIIIIIXGGGGGG
................CCCCCCCCXBBBBBBBBBBBBBXIIIIIIIIIIIIIXGGGGGGG
...............CCCCCCCCCCXBBBBBBBBBBBBXIIIIIIIIIIIIIXGGGGGGG
..............CCCCCCCCCCCCXBBBBBBBBBBBXIIIIIIIIIIIIIXGGGGGGG
.............CCCCCCCCCCCCCCXBBBBBBBBBBBIIIIIIIIIIIIIXGGGGGGG
............CCCCCCCCCCCCCCCCXBBBBBBBBB..IIIIIIIIIIIIXXXXGGGG
...........CCCCCCCCCCCCCCCCCCXBBBBBBB....IIIIIIIIIIXDDDDXGGG
..........CCCCCCCCCCCCCCCCCCCCXBBBBB......IIIIIIIIXDDDDDDXGG
...........CCCCCCCCCCCCCCCCCCCBBBBB......DDIIIIIIXDDDDDDDDXG
............CCCCCCCCCCCCCCCCCBBBBB......DDDDIIIIXDDDDDDDDDDX
.............CCCCCCCCCCCCCCC..BBB......DDDDDDIIXDDDDDDDDDDDD
..............CCCCCCCCCCCCC....B......DDDDDDDDXDDDDDDDDDDDDD
...............CCCCCCCCCCC...........DDDDDDDDDDDDDDDDDDDDDDD
Second map
................CCCCCCCCC...........DDDDDDDDDDDDDDDDDDDDDDDD
.................CCCCCCC...........DDDDDDDDDDDDDDDDDDDDDDDDD
..................CCCCC.............DDDDDDDDDDDDDDDDDDDDDDDD
...................CCC...............DDDDDDDDDDDDDDDDDDDDDDD
....................C.................DDDDDDDDDDDDDDDDDDDDDD
.......................................DDDDDDDDDDDDDDDDDDDDD
Appendices (Past Assignment 6 for the reference of Part 2)
This is for your reference only. This is NOT a part of the PE.
(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.