0% found this document useful (0 votes)
15 views

cs1010s Final Apr21 PDF

Uploaded by

purnamakay
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)
15 views

cs1010s Final Apr21 PDF

Uploaded by

purnamakay
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/ 24

NATIONAL UNIVERSITY OF SINGAPORE

CS1010S—Programming Methodology
2020/2021 Semester 2

Time Allowed: 2 hours

INSTRUCTIONS TO STUDENTS

1. Please write your Student Number only. Do not write your name.
2. The assessment paper contains FIVE (5) questions and comprises TWELVE (12) pages
including this cover page.
3. Weightage of each question is given in square brackets. The maximum attainable score
is 100.
4. This is a CLOSED book assessment, but you are allowed to bring ONE double-sided
A4 sheet of notes for this assessment.
5. Write all your answers in the space provided in the ANSWER BOOKLET.
6. You are allowed to write with pencils, as long as it is legible.
7. Marks may be deducted for i) unrecognisable handwriting, and/or ii) excessively long
code. A general guide would be not more than twice the length of our model answers.
8. Common List and Dictionary methods are listed in the Appendix for your reference.
CS1010S Final Assessment

This page is intentionally left blank.


It may be used as scratch paper.

2
CS1010S Final Assessment

Question 1: Python Expressions [30 marks]


There are several parts to this problem. Answer each part independently and separately.
In each part, one or more Python expressions are entered into the interpreter (Python shell).
Determine the response printed by the interpreter for the final expression entered and write the
exact output in the answer box. If the interpreter produces an error message, or enters an
infinite loop, explain why and clearly state the responsible evaluation step.
The code is replicated on the answer booklet. You may show your workings outside the answer
box in the space beside the code. Partial marks may be awarded for workings even if the final
answer is wrong.

A. a = [0] D. x, o = "x", "o"


b = [1, [2]] def f():
a.append(b[1]) return x + o
print(a) def g(x, o):
a.extend(b) x = f()
print(a) print(x)
a[1] = 3 print(o)
b[1] = 4 o = f()
print(a) return x + o
[5 marks] print(g(o, x))
[5 marks]
B. d = {1: 2, 2: 0, 3: 1}
def descend(v): E. def caution(x):
if v > 0: try:
descend(d[v]) return x[2] * 2
print(v) except IndexError:
descend(3) return x
[5 marks] except TypeError:
return "type"
print( caution("CS1010S") )
C. s = "lever"
print( caution([1, 2]) )
while s:
print( caution({1: 2}) )
print(s)
if s[0] == s[-1]: [5 marks]
s = s[1:-1]
elif s[0] not in s[1:]: F. def f(x):
s = s[1:] print(x)
else: return lambda y: g(y) + x
s = s[:-1] def g(x):
[5 marks] print(x)
return lambda y: y + x
print(f(1)(2)(3))
[5 marks]

3
CS1010S Final Assessment

Question 2: Blockchain [22 marks]


Digital COVID-19 vaccination certificates in Malaysia and Singapore are secured by blockchain
technology and come with a traceability feature that tells the exact batch of the vaccine vial
used for inoculation. Source: Channel NewsAsia
For this question, we represent the blockchain as a chain of blocks. Each block is represented
as a list containing the name of the person who has been vaccinated, the date of the vacci-
nation, and a string hash. For example, on this particular chain, there are four people who have
been vaccinated:
blockchain = [
["Ben", "2021-02-28", "815"], ["Kenghwee", "2021-04-01", "1456"],
["Waikay", "2021-04-17", "1313"], ["Jon", "2021-04-29", "989"]
]

A. [Warm-up] The hash function is an essential component of all blockchains. Since we have
not studied cryptography in CS1010S, we settle for a very simple and naive hash function.
Implement the function hash(string) that takes as input a string ( str ). Return the sum of
all the ordinals of each character, converted to a string (instead of an integer). You may use the
ord function for this question. [4 marks]
Sample execution:
>>> ord("D")
68
>>> ord("d")
100
>>> hash("Dd")
"168"
>>> hash("dec")
"300"

B. Each block in the blockchain contains a name ( str ), a date ( str ), and a hash ( str ). A
blockchain could be arbitrarily long.
Implement the function, get_names(blockchain, date) that takes as input a blockchain
( list of list ), and a date ( str ). Return the list of all the names in the given blockchain
who were vaccinated before or equal to the given date. Return the names in the same order of
appearance in the blockchain. [4 marks]
Note: Since the dates are in ISO date format, you can directly compare the strings
Sample execution:
>>> "2021-01-01" < "2021-04-01" # 1st Jan 2021 before 1st Apr 2021
True # ISO format allows for direct comparison

>>> get_names(blockchain, "2021-04-01")


["Ben", "Kenghwee"]

4
CS1010S Final Assessment

C. The hash of each block in the blockchain is derived by concatenating the name and date of
the current block, with the hash of the previous block. This way, a block in the chain cannot be
modified without affecting the entire chain.
For example, adding the name "Eshin" on the date "2021-04-29" to the blockchain in the
example above will modify and return the existing blockchain to include a new block:
>>> add_block("Eshin", "2021-04-29", blockchain)
>>> blockchain
[["Ben", "2021-02-28", "815"], ["Kenghwee", "2021-04-01", "1456"],
["Waikay", "2021-04-17", "1313"], ["Jon", "2021-04-29", "989"],
["Eshin", "2021-04-29", "1167"]]

The new hash value "1167" was generated from the hash of the strings "989", "Eshin", and
"2021-04-29" concatenated together.

Implement the function add_block(name, date, blockchain) that appends a new block
containing the name, date and correct hash value to the end of the blockchain. You may assume
that the blockchain contains at least one block. [4 marks]

D. Using blockchain technology allows us to verify the validity of the blocks in the chain.
Implement the function is_valid(blockchain) that returns True if the blockchain is valid,
and False otherwise. A blockchain is valid if the hash values of each block matches the
expected computed hash value. The previous hash value of the first block is assumed to be an
empty string "". [4 marks]

E. In order to verify if a person has been vaccinated, we need a way to retrieve the date of
vaccination. Implement the function date_vaccinated(name, blockchain) that returns
the date when the person was innoculated with the vaccine, or None if the blockchain is not
valid or if the person does not have any record on the blockchain. You may assume that names
are unique. [4 marks]

F. The current implementation of hash not good since Waikay can be substituted with his evil
twin Wayaki, and the blockchain will still be valid. In fact, Wayaki gleefully demonstrates this:
blockchain[2][0] = "Wayaki"

In order to prevent changes in the names in the blockchain, Ben Bitdiddle decides to change
the implementation of the blockchain to list of tuples.
improved_blockchain = [
("Ben", "2021-02-28", "815"), ("Kenghwee", "2021-04-01", "1456"),
("Waikay", "2021-04-17", "1313"), ("Jon", "2021-04-29", "989")
]

Is this implementation now safe from any form of tampering by the evil twin Wayaki? If this is
safe, state yes and explain why. Otherwise, state no and demonstrate with some code how the
blockchain may still be tampered. [2 marks]

5
CS1010S Final Assessment

Question 3: Scrabble Tiles [28 marks]


Scrabble is a word game in which two to four players score points by placing tiles, each bearing
a single character, onto a game board divided into a 15⇥15 grid of squares. English-language
editions of Scrabble contain 100 tiles, representing the following distribution of characters:

blank ⇥2 D ⇥4 H ⇥2 L ⇥4 P ⇥2 T ⇥6 X ⇥1
A ⇥9 E ⇥12 I ⇥9 M ⇥2 Q ⇥1 U ⇥4 Y ⇥2
B ⇥2 F ⇥2 J ⇥1 N ⇥6 R ⇥6 V ⇥2 Z ⇥1
C ⇥2 G ⇥3 K ⇥1 O ⇥8 S ⇥4 W ⇥2

Source: Wikipedia
We can represent a bag of tiles of the current distribution using a list-of-lists, where the index
of the outer list represent the number of tiles of each of the characters in the inner list. E.g., the
above distribution will be represented as follows (with space representing blank tiles):
bag = [[],
['K', 'J', 'X', 'Q', 'Z',],
[' ', 'F', 'H', 'V', 'W', 'Y', 'B', 'C', 'M', 'P'],
['G'],
['D', 'L', 'S', 'U'],
[],
['N', 'R', 'T'],
[],
['O'],
['A', 'I'],
[],
[],
['E']]

During the game, tiles are drawn from the bag, which changes the distribution of the remaining
characters.

A. [Warm-up] Implement the function total_tiles(bag) that takes as input a bag of tiles
in the above-mentioned representation, and returns the total number of tiles currently in the
bag. [4 marks]

B. The function remove(char, bag) takes as input a character, and a bag of tiles (in the
above-mentioned representation). If the bag contains one or more of the given character, one
tile of the character is removed and the bag is updated to represent this new distribution, and
True is returned. Otherwise, if the bag does not contain the character, False is returned.

Provide an implementation of the function remove . Note there is no need to remove empty
lists from the outer list. [4 marks]

6
CS1010S Final Assessment

C. The current list-of-list used to represent the distribution of tiles in a bag is not very efficient.
It would be better if we can use a dictionary to represent the distribution using the characters
as keys, and the values as their quantity.
For example, the initial distribution of the English-language Scrabble would be:
{' ': 2, 'A': 9, 'B': 2, 'C': 2, 'D': 4, 'E': 12, 'F': 2, 'G': 3, 'H': 2,
'I': 9, 'J': 1, 'K': 1, 'L': 4, 'M': 2, 'N': 6, 'O': 8, 'P': 2, 'Q': 1,
'R': 6, 'S': 4, 'T': 6, 'U': 4, 'V': 2, 'W': 2, 'X': 1, 'Y': 2, 'Z': 1}

Implement the function to_dict(bag) which takes in a bag in a list-of-list representation of


the tiles, and returns a dictionary representation as described. [4 marks]

D. Implement the function to_list(bag) , that takes in a bag of tiles in the dictionary rep-
resentation, and returns a bag in the list-of-list representation. Basically it does the reverse of
the to_dict function. Hint: take note of aliasing when creating an empty list-of-lists.
[4 marks]

E. Sometimes players want to play Mega Scrabble by combining tiles of different bags into
one bag. Implement the function combine(a, b) that takes two bags of tiles in the list-of-lists
representation, and returns a new bag of tiles, also in the list-of-list representation.
[6 marks]

F. It is not good to have a global variable represent a bag. Instead, the global bag variable
should be used to “clone” new bags which can be used for different Scrabble games. To prevent
players from peeking into these cloned bags, we can hide them inside a function.
The function new_game(bag) takes as input a bag of tiles (in whichever representation you
wish to use), and returns a function that draws a random tile from a “cloned” bag and returns
the character drawn. The tile is removed from the “cloned” bag, and not from the original bag
given. If the bag has no more tiles, False is returned. For example:
>>> game = new_game(bag)
>>> game()
'L' # letter drawn at random

>>> for i in range(99):


... x = game() # draw 99 more tiles and suppress output
>>> game()
False # no more tiles
_
>>> total tiles(bag)
100 # original bag is untouched

Given the function choice(seq) that takes in a sequence and returns a random element of the
sequence, provide an implementation for the function new_game . Note that you are not to use
any other randomisation function other than choice . [6 marks]

7
CS1010S Final Assessment

Question 4: My Little Pony [16 marks]


Consider the following classes:
1 class Pony:
2 def __init__(self, energy):
3 self.energy = energy
4
5 def consume(self):
6 self.energy = max(0, self.energy - 10)
7
8 def move(self):
9 if self.energy == 0:
10 print("Need to rest")
11 else:
12 print("Let's go!")
13 self.consume()
14
15
16 class Pegasus(Pony):
17 def fly(self):
18 self.flying = True
19
20 def land(self):
21 self.flying = False
22
23 def consume(self):
24 super().consume()
25 if (self.flying):
26 super().consume()

A. Clarence found something odd with the current implementation. He gets an error when he
executes the following lines:
>>> rainbow_dash = Pegasus(20)
>>> rainbow_dash.move()
Let's go!
xxxxError: xxxxx

But if he gets his pony to fly first, he does not get any error.
>>> rainbow_dash = Pegasus(20)
>>> rainbow_dash.fly()
>>> rainbow_dash.land()
>>> rainbow_dash.move()
Let's go!

Explain the cause of the error and why it does not occur in the latter code. Implement the code
to fix this according to OOP principles, i.e. redundant code will be penalised. [4 marks]

8
CS1010S Final Assessment

B. A Unicorn is a Pony that has magical powers. It has a special reserve of mana energy
that allows it to be consumed in lieu of its regular “Pony energy” when the consume method is
called. This mana energy is only good for 3 uses. Once her mana energy is used up, the normal
behaviour of consumption energy resumes.
Example:
>>> rarity = Unicorn(10)
>>> rarity.move() # consumes mana
Let's go!
>>> rarity.consume() # consumes mana
>>> rarity.consume() # consumes mana
>>> rarity.energy
10 # still has 10 energy
>>> rarity.move()
Let's go!
>>> rarity.move()
Need to rest

Provide an implementation of the class Unicorn using OOP principles. Redundant code or
methods will be penalised. [6 marks]

C. An Alicorn is a combination of both Pegasus and Unicorn ponies. It is able to fly as


a Pegasus and have the same magical powers as a Unicorn.
Twilight Sparkle thinks that she can use OOP polymorphic behaviour to implement Alicorn
with simply:
class Alicorn(Pegasus, Unicorn):
pass

She is also unsure whether the order of the super classes Pegasus and Unicorn makes any
difference.
Sunset Shimmer thinks Twilight is being silly as classes should always contain methods to
work. And besides, the ordering of the super classes makes no difference here.
Explain in detail if Twilight Sparkle’s idea is correct, and whether the order of the super classes
matter. You can list some sample execution to aid your explanation. [6 marks]

Question 5: 42 and the Meaning of Life [4 marks]


Either: (a) explain how you think some of what you have learnt in CS1010S will be helpful for
you for the rest of your life and/or studies at NUS; (b) tell us an interesting story about your
experience with CS1010S this semester; or (c) share how the COVID 19 situation has positively
influenced/impacted your learning in CS1010S. [4 marks]

— END OF PAPER —

9
CS1010S Final Assessment

Appendix
Parts of the Python documentation is given here for your reference.
List Methods
• list.append(x) Add an item to the end of the list.
• list.extend(iterable) Extend the list by appending all the items from the iterable.
• list.insert(i, x) Insert an item at a given position.
• list.remove(x) Remove the first item from the list whose value is x. It is an error if
there is no such item.
• list.pop([i]) Remove the item at the given position in the list, and return it. If no
index is specified, removes and returns the last item in the list.
• list.clear() Remove all items from the list
• list.index(x) Return zero-based index in the list of the first item whose value is x.
Raises a ValueError if there is no such item.
• list.count(x) Return the number of times x appears in the list.
• list.sort(key=None, reverse=False) Sort the items of the list in place.
• list.reverse() Reverse the elements of the list in place.
• list.copy() Return a shallow copy of the list.
Dictionary Methods
• dict.clear() Remove all items from the dictionary.
• dict.copy() Return a shallow copy of the dictionary.
• dict.items() Return a new view of the dictionary’s items ( (key, value) pairs).
• dict.keys() Return a new view of the dictionary’s keys.
• dict.pop(key[, default]) If key is in the dictionary, remove it and return its value,
else return de f ault. If de f ault is not given and key is not in the dictionary, a KeyError
is raised.
• dict.update([other]) Update the dictionary with the key/value pairs from other,
overwriting existing keys. Return None .
• dict.values() Return a new view of the dictionary’s values.

10
CS1010S Final Assessment

Scratch Paper

11
CS1010S Final Assessment

Scratch Paper

— H A P P Y H O L I D A Y S ! —

12
CS1010S — Programming Methodology
School of Computing
National University of Singapore

Final Assessment — Answer Sheet


2020/2021 Semester 2

Time allowed: 2 hours

Instructions (please read carefully): STUDENT NUMBER

1. Write down your student number on the right and us- A


ing ink or pencil, shade the corresponding circle com- U 0 0 0 0 0 0 0 A N
A 1 1 1 1 1 1 1 B R
pletely in the grid for each digit or letter. DO NOT
HT 2 2 2 2 2 2 2 E U
WRITE YOUR NAME! NT 3 3 3 3 3 3 3 H W
4 4 4 4 4 4 4 J X
2. This answer booklet comprises TWELVE (12) pages,
5 5 5 5 5 5 5 L Y
including this cover page. 6 6 6 6 6 6 6 M

3. All questions must be answered in the space provided; 7 7 7 7 7 7 7


8 8 8 8 8 8 8
no extra sheets will be accepted as answers. You may 9 9 9 9 9 9 9
use the extra page behind this cover page if you need
more space for your answers.
4. You must submit only the ANSWER SHEET and no For Examiner’s Use Only
other documents. The question set may be used as Question Marks
scratch paper.
Q1 / 30
5. An excerpt of the question may be provided to aid you
in answering in the correct box. It is not the exact Q2 / 22
question. You should still refer to the original question
in the question booklet. Q3 / 28

6. You are allowed to use pencils, ball-pens or fountain Q4 / 16


pens, as you like as long as it is legible (no red color,
please). Q5 / 4

7. Marks may be deducted for i) unrecognisable hand- Total /100


writing, and/or ii) excessively long code. A general
guide would be not more than twice the length of our
model answers.
Answer Sheet CS1010S Final Assessment

This page is intentionally left blank.


Use it ONLY if you need extra space for your answers, and indicate the question number
clearly as well as in the original answer box. Do NOT use it for your rough work.

2
Answer Sheet CS1010S Final Assessment

Question 1A [5 marks]
a = [0]
b = [1, [2]]
a.append(b[1])
print(a)
a.extend(b)
print(a)
a[1] = 3
b[1] = 4
print(a)

Question 1B [5 marks]
d = {1: 2, 2: 0, 3: 1}
def descend(v):
if v > 0:
descend(d[v])
print(v)
descend(3)

Question 1C [5 marks]
s = "lever"
while s:
print(s)
if s[0] == s[-1]:
s = s[1:-1]
elif s[0] not in s[1:]:
s = s[1:]
else:
s = s[:-1]

3
Answer Sheet CS1010S Final Assessment

Question 1D [5 marks]
x, o = "x", "o"
def f():
return x + o
def g(x, o):
x = f()
print(x)
print(o)
o = f()
return x + o
print(g(o, x))

Question 1E [5 marks]
def caution(x):
try:
return x[2] * 2
except IndexError:
return x
except TypeError:
return "type"
print( caution("CS1010S") )
print( caution([1, 2]) )
print( caution({1: 2}) )

Question 1F [5 marks]
def f(x):
print(x)
return lambda y: g(y) + x
def g(x):
print(x)
return lambda y: y + x
print(f(1)(2)(3))

4
Answer Sheet CS1010S Final Assessment

Question 2A [4 marks]

def hash(string):

Question 2B [4 marks]

def get_names(blockchain, date):

Question 2C [4 marks]

def add_block(name, date, blockchain):

5
Answer Sheet CS1010S Final Assessment

Question 2D [4 marks]

def is_valid(blockchain):

Question 2E [4 marks]

def date_vaccinated(name, blockchain):

Question 2F Explain or demonstrate whether the new representation is safe [2 marks]

6
Answer Sheet CS1010S Final Assessment

Question 3A [4 marks]

def total_tiles(bag):

Question 3B [4 marks]

def remove(char, bag):

Question 3C [4 marks]

def to_dict(bag):

-1 mark for calling non-existent dict.sort()


-1 mark for using index(row) instead of in range(len(bag))
-2 marks hard-coding the range
-3 marks putting the wrong values in the dict

7
Answer Sheet CS1010S Final Assessment

Question 3D [4 marks]

def list(bag):

Question 3E [6 marks]

def combine(a, b):

8
Answer Sheet CS1010S Final Assessment

Question 3F [6 marks]

def new_game(bag): # my bag is list-of-list / dictionary (delete accordingly)

Question 4A Explain the error and implement a solution. [4 marks]

9
Answer Sheet CS1010S Final Assessment

Question 4B Implement the class Unicorn . [6 marks]

Question 4C Explain in detail if Twilight Sparkle is correct. [6 marks]

10
Answer Sheet CS1010S Final Assessment

Question 5 42 and the Meaning of Life. [4 marks]

11
Answer Sheet CS1010S Final Assessment

This page is intentionally left blank.


Use it ONLY if you need extra space for your answers, and indicate the question number
clearly as well as in the original answer box. Do NOT use it for your rough work.

— END OF ANSWER SHEET —

12

You might also like