0% found this document useful (0 votes)
9 views11 pages

Prev Year PE2

Uploaded by

taytsemintay
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)
9 views11 pages

Prev Year PE2

Uploaded by

taytsemintay
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/ 11

CS1010E Practical Exam 2

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.

• Please be reminded that


you are not allowed to
use any global variable
Part 1 Fibonacci Numbers (10+10+10+10=40 marks)
In our lecture, we learned about Fibonacci numbers. We can generalize the concept into any order. In this part, you do
not need to consider efficiency. Namely, no need to use memorization. However, please note that every task in this part
has to be independent. Meaning, e.g. you are not supposed to call a function in Task 3 for your Task 1 solution. This
means that it is ok to have some redundancies in your code in all the tasks. Also, you can assume the input i in all tasks
are larger than or equal to 0.

Task 1 Fibonacci Number of Order 3 (Recursion version)


We define the Fibonacci numbers of order 3 as following. The 0th Fibonacci number F0 is 0, and the 1st and second
Fibonacci numbers are F1, F2 = 1. And the ith Fibonacci number Fi is,

Fi = Fi-1 + Fi-2 + Fi-3.


E.g. the series starting from F0 is:
0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, …

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

Task 2 Fibonacci Number of Order 3 (Iterative version)


Write an iterative version fib3_i(i) of the function in Task 1, with the same functionality. Your function cannot be
recursive in this task.

Task 3 Fibonacci Number of Order N (Recursion version)


A Fibonacci sequence of order n (n > 1) is an integer sequence in which each sequence element is the sum of the
previous n elements (with the exception of the first n elements in the sequence). The 0th Fibonacci number F0 is 0. And
the 1st to the (n - 1)th Fibonacci numbers are 1, namely, F1, F2, F3, … , Fn-1 = 1. For example, if n = 5, the sequence,
starting from F0, will be:
0, 1, 1, 1, 1, 4, 8, 15, 29, 57, 113, 222, 436, 857, 1685

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

Task 4 Fibonacci Number of Order n (Iterative version)


Write an iterative version fibn_i(i) of the function in Task 3, with the same functionality. Your function cannot be
recursive in this task.
Part 2 Pizza Delivery 2.0 (30 marks)
Yes, you did not read it wrong, this task is similar to Assignment 6. If you forgot what is 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 by
defining a class Pizza_shop with the following attributes (Please read the details in the given code template):

• x,y: the position/coordinates as in Assignment 6


• name: The name of the pizza shop in a string
• max_dist: The robot can only deliver pizza for a distance less than or equal to this max_dist.

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)

Task 1 Pizza Delivery Map 2.0


Write a function pizza_map(r,c,shops) to compute the pizza shop delivery map similar to Assignment 6 with the
same return value format, namely, a 2D array of symbols, with the following parameters:

• r,c : Same as Assignment 6, the number of row and column of the map
• shops: a list of instances of class Pizza_Shop

And some modifications from the original PDMap from Assignment 6:

• 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 following three maps are the results of:

mtp(pizza_map(10,12,shops[:1])) # left: only the first pizza shop


mtp(pizza_map(10,12,shops[1:])) # middle: only the second pizza shop
mtp(pizza_map(10,12,shops)) # right: with both the pizza shops

… respectively. (The function ‘mtp’ is the same function ‘mTight_print()’)

...A........ ............ ...A........


..AAA....... ......B..... ..AAA.B.....
.AAAAA...... .....BBB.... .AAAAABB....
AAAAAAA..... ....BBBBB... AAAAAAXBB...
.AAAAA...... ...BBBBBBB.. .AAAAXBBBB..
..AAA....... ..BBBBBBBBB. ..AAXBBBBBB.
...A........ .BBBBBBBBBBB .BBXBBBBBBBB
............ ..BBBBBBBBB. ..BBBBBBBBB.
............ ...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.

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:

● He will only choose to move in the north or east direction.


● After he finish moving one unit of distance, he will chose a direction to go for another unit of distance.

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)

Task 1 Compute NE Paths


Write a function n_ways(A, B) to return the number of ways he can take from (0, 0) to (A, B). Here are some
examples.

>>> 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:

>>> print(n_ways_with_blocks (0,0,()))


1
>>> print(n_ways_with_blocks (0,0,((0,0),)))
0
>>> block = ((2,3),(0,3),(6,9),(5,11))
>>> print(n_ways_with_blocks (6,11,block))
1131
>>> block3 = block + ((1,4),(3,4),(4,5))
>>> print(n_ways_with_blocks (6,11,block3))
71
>>> print(n_ways_with_blocks (40,50,block3))
13775028282698978194313826
Template and Test Cases (Copy and paste each of the following into ONE .py file. )
###############################################################
#Part 1
def fib3_r(i):
pass
def fib3_i(i):
pass
def fibn_r(i,n):
pass
def fibn_i(i,n):
pass
# You should not submit the following code. They are for your testing only
print(fib3_r(5)) # ans:7
print(fib3_r(11)) # ans:274
print(fib3_r(20)) # ans:66012
print(fibn_r(13,5)) # ans: 857
print(fibn_r(21,4)) # ans: 207529
print(fibn_r(14,7)) # ans: 705

################################################################
#Part 2
def pizza_map(r,c,shops):
return pass

# You should not submit the following code.

class Pizza_Shop: # You do not need to submit this code


def __init__(self,x,y,max_dist,name):
self.x = x
self.y = y
self.max_dist = max_dist
self.name = name

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.

Assignment 6: Lightning Pizza Delivery


In a town called Regulaville, and all the people living there have very strange habits. All the buildings have centers on a
well-structured rectangular grid. The mayor of the town is at the most north-west corner of the town, and his house
coordinates are (0,0). And each building in town has coordinates (i,j) that indicate that his house is i km south and
j km east from the mayor’s house for i and j are integers such that 0  i < h and 0  j < w for some integers h
and w.

(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().

>>> pizzaMap = PDMap(7,8,[[1,3],[4,7],[7,2]]) >>> mTightPrint(pizzaMap)


>>> pprint(pizzaMap) 00000001
[[0, 0, 0, 0, 0, 0, 0, 1], 00000001
[0, 0, 0, 0, 0, 0, 0, 1], 00000011
[0, 0, 0, 0, 0, 0, 1, 1], 00000111
[0, 0, 0, 0, 0, 1, 1, 1], 22201111
[2, 2, 2, 0, 1, 1, 1, 1], 22222111
[2, 2, 2, 2, 2, 1, 1, 1], 22222111
[2, 2, 2, 2, 2, 1, 1, 1]]

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.

You might also like