Mock Mid-Terms
Mock Mid-Terms
A. x , y = 3 , 12 D. n = 420
def f (x , y ): while n >= -1 :
x += y print ( n )
y %= x n //= 3
return x * y if n % 2 == 0 :
z , y = f (y , x ) , 1 print ( 2 * n )
print ( f (y , z - x )) continue
elif n % 7 == 1 :
[5 marks] print ( n / 2 )
break
B. p , q = () , (7 , 4 , 2 , 5 , 3 )
n //= 2
for r in q :
n -= 2
if r % 5 == 2 :
p = ( -1 , r ,) + p [ 1 :]
[5 marks]
elif r % 2 == 0 :
p = (r ,) + p + (r , -1 ) E. def con (x , y ):
print ( p ) return lambda z : y (x , z )
def fuse ( x ):
[5 marks] return lambda y : x (y , y )
why = lambda x , y : x * y
C. m , p = " mutton " , " python "
so = lambda x , y : x ** 2 + y
mp = m + p + " ton "
hard = lambda y , x : 2 * x + y
if " on " in mp :
dif = con ( fuse ( so )( 3 ) , why )
if p in mp :
print ( dif ( fuse ( hard )( 2 )))
p = "p"
print ( " yum " ) [5 marks]
if " on " in p :
print ( " bum " )
elif mp :
print ( " tum " )
[5 marks]
CS1010S—Mock Midterm 2
Suppose we are to implement a function choose that takes in two nonnegative integer
inputs n, k and returns
n n!
=
k k!(n − k)!
i.e. the number of ways to pick k items out of n items.
For the following questions you may assume that k ≤ n.
A. Provide a recursive implementation of the function choose . For example, choose(10,
3) will return 120. Note that you are not allowed to use any additional functions.
Hint: Divide into two cases whether you picked the k-th item or not. How many
items left to choose for each cases?
[4 marks]
B. State the order of growth in terms of time and space for the function you wrote in
Part (A). Briefly explain your answer.
[3 marks]
C. Provide an iterative implementation of the function choose . Note that you are not
allowed to use any additional functions.
Hint: Note that you can express nk like the following.
n n! n · (n − 1) · . . . · (n − k + 1)
= =
k k!(n − k)! 1 · 2 · ... · k
[4 marks]
D. State the order of growth in terms of time and space for the function you wrote in
Part (C). Briefly explain your answer.
[2 marks]
E. Now that we have defined the function choose , we can find the probability that the
random variable X has a value less than or equal to some nonnegative integer k.
Mathematically,
P (X ≤ k) = P (X = 0) + P (X = 1) + . . . + P (X = k)
n 0 n−0 n 1 n−1 n k
= p (1 − p) + p (1 − p) + ... + p (1 − p)n−k
0 1 k
CS1010S—Mock Midterm 3
P (X ≤ 3) = P (X = 0) + P (X = 1) + P (X = 2) + P (X = 3)
10 0 10−0 10
= 0.5 (1 − 0.5) + 0.51 (1 − 0.5)10−1
0 1
10 2 10−2 10
+ 0.5 (1 − 0.5) + 0.53 (1 − 0.5)10−3
2 3
10 10 10 10
= + + + (0.5)10 s
0 1 2 3
= (1 + 10 + 45 + 120)(0.5)10
176
= = 0.171875
1024
Using the previously defined functions, provide an implementation to the function
cumulative_prob . You may assume that the given input will be a valid input, e.g.
the probability given will lie in the [0, 1] interval.
[4 marks]
F. Is your implementation in Part (E) recursive or iterative? State the order of growth
in terms of time and space of your implementation. Briefly explain your answer.
[2 marks]
CS1010S—Mock Midterm 4
Please provide possible implementations for the terms T1, T2, and T3. You may op-
tionally define other functions in PRE if needed.
Note: You are to use the higher-order function and not solve it recursively or it-
eratively or use a formula.
[6 marks]
B. Consider the higher-order function sum which was taught in class.
def sum ( term , a , next , b ):
if a > b :
return 0
else :
return term ( a ) + sum ( term , next ( a ) , next , b )
It turns out that the function cumulative_prob defined in Question 2 can also be
defined in terms of sum as follows:
def cumulative_prob (n , p , k ):
< PRE2 >
return sum ( < T4 > , <T5 > , <T6 > , <T7 >)
Please provide possible implementations for the terms T4, T5, T6, and T7. You may
optionally define other functions in PRE2 if needed.
Note: You are to use the higher-order function and not solve it recursively or it-
eratively or use a formula.
[4 marks]
CS1010S—Mock Midterm 5
Background [OK to skip] Brawl Stars is a multiplayer online battle arena and third-
person hero shooter video game developed and published by the Finnish video game
company Supercell. It was released worldwide on December 12, 2018 on iOS and An-
droid. The game features various game modes, each with a different objective. Players
can choose from a selection of Brawlers, which are characters that can be controlled
with on-screen joysticks in a game match. In Brawl Stars, players battle against other
players or AI opponents in multiple game modes. Players can choose between charac-
ters called Brawlers that they have unlocked through Boxes, the Brawl Pass, the Trophy
Road, or purchased through the Shop to use in battles.
Source: Wikipedia
You are so addicted to Brawl Stars you decided to implement a Brawl Stars account in
Python. You hope that implementing such thing can help you study CS1010S and play
Brawl Stars at the same time.
Your first step is to design an account data type that stores the amount of coins the
account currently has and the brawlers (Brawl Stars characters) unlocked along with
the power points obtained for each brawler. Your account data type should support the
following functions:
• make_account takes in no parameters and returns a new fresh account with 1000
coins at the start.
• add_coins(account, amt) returns a new account with amt coins added to the ac-
count.
• obtain_powerpoint(account, brawler, points) returns a new account with points
power points added to brawler’s statistics. If brawler hasn’t been unlocked before,
add it to the collection. However, 2 coins are deducted for each power point.
• get_coins(account) returns the amount of coins the account currently has.
• get_brawlers(account) returns a tuple of all the existing brawlers’ names from the
account.
• get_level(account, brawler) returns the level of the brawler in the account based
on the following table. If the brawler does not exist in the account, assume it has 0
power points.
Level Points
0 0
1 1-19
2 20-49
3 50-99
4 100-199
5 200-549
6 550
CS1010S—Mock Midterm 6
Example execution:
>>> acc = make_account ()
>>> acc2 = add_coins ( acc , 300 )
>>> acc3 = obtain_powerpoint ( acc2 , " Shelly " , 125 )
>>> get_coins ( acc3 )
1050
>>> get_level ( acc3 , " Shelly " )
4
Modify the obtain_powerpoint function such that it can cater to this problem.
Again, you may assume that the power points that you have to pay for will not
exceed your current coins balance.
[4 marks]
CS1010S—Mock Midterm 7
Appendix
The following are some functions that were introduced in class. For your reference, they
are reproduced here.
def sum ( term , a , next , b ):
if a > b :
return 0
else :
return term ( a ) + sum ( term , next ( a ) , next , b )
def fold ( op , f , n ):
if n == 0 :
return f ( 0 )
else :
return op ( f ( n ) , fold ( op , f , n - 1 ))