Exam 2023s1 Main Solutions
Exam 2023s1 Main Solutions
Instructions to Students: This exam contains 100 marks, and counts for 50% of your final grade.
Be sure to write your student number clearly in all places where it is requested.
This assessment is closed book, and you may not make use of any printed, written, electronic, or
online resources.
All questions should be answered in the spaces provided on the exam paper. There is also an
overflow page available at the end of the paper. If you write any answers there, be sure to also write
the corresponding question number.
There are a number of places where you are asked for a single Python statement, but two or more
answer lines are provided. In these cases you may draft your answer in one line and then copy it
neatly to another line within the answer boxes if you wish, but if you do so you must then cross out
the first draft answer.
You must not communicate with any other student in any way from the moment you enter the exam
venue until after you have left the exam venue. All phones and other network, communication, and
electronic devices must be switched completely off while you are in the exam room.
All material that is submitted as part of this assessment must be completely your own unassisted
work, and undertaken during the time period allocated to the assessment.
Calculators and dictionaries are not permitted.
In your answers you may make use of built-in functions, but may not import functions from any
other libraries.
You may use any blank pages to prepare drafts of answers, but you must copy those answer onto
the correct answer box before the end of the exam.
Question 1 (20 marks)
Each subquestion is worth 2 marks. To get full marks you must specify both the correct value and
be clear in your answer that you understand what the type will be. If you think an expression is not
legal according to the rules of Python, you should write “Error” in the box. If you want to include
any blanks in output strings, draw one symbol for each blank character.
(a) 2 + 3 * 4 // 5 4
(b) 4 - 12 % 5 * 3 -2
Each subquestion is worth 4 marks. There are two lines provided for each answer, but you should
give a single Python assignment statement if you can. If your answer requires more than one
assignment statement you will be eligible for partial marks.
(a) Suppose that vals is a Python list. Give a Python assignment statement that assigns True to
even size if vals has an even number of items in it, and assigns False if not.
(b) Suppose that vals is a Python list of numbers. Give a Python assignment statement that
assigns True to all equal if all of the values in vals are the same, and assigns False if not.
(c) Suppose that n is a positive integer. Give a Python assignment statement that creates a list
list of tup containing n tuples, with each tuple containing n values all of which are zeros.
(d) Suppose that text is a Python string. Give a Python assignment statement that assigns the
number of digit characters in text to the variable n digits.
(e) Suppose that nums is a Python list of numbers. Give a Python assignment statement that cre-
ates a new version of nums in which 1 has been added to the first element in nums, 2 has been
added to the second element, 3 to the third element, and so on through the remaining elements.
The following function reformats text so that input lines of any length are formed into a paragraph
in which all of the lines are of roughly equal length, except for the last one.
XXXXXXXXXX # 08
# try and fit the next word
XXXXXXXXXX # 09
# can’t place next word, so need to start a new line
out_lines.append(out_line) # 10
out_line = "" # 11
all words = []
words = line.split()
Consider the following Python function, which is designed to find the most frequently occurring
vowel in some given string text. There are five vowels in English: a, e, i, o, and u.
def most_freq_vowel(text): # 01
’’’
Return the most frequently occurring vowel in ‘text‘ in a case
insensitive manner (that is, ’a’ should be counted the same as
’A’). If there is a tie, return the vowel that appears first
in the English alphabet, and if there are no vowels at all,
return None.
’’’
vowels = "eeiou" # 02
counts = {} # 03
if len(counts) != 0: # 07
return None # 08
max_count = max(counts.values()) # 09
return None # 13
Unfortunately, the function contains a number of errors. The subquestions on the next two pages
are designed to help you first identify those errors, and then correct them.
most_freq_vowel("hello")
to determine what is returned. Show both the value and type in the answer that you provide. Or,
if you think an exception will be raised before the function returns, indicate in English what error
will arise (for example, “divide by zero”, or “index out of range”, or “invalid method for type int”,
and so on).
Now write the output that should have been returned for that input if the function was working
correctly.
’e’
Finally, identify a minimal fix for this error, by giving exactly one line number in the code that is
to change, and then providing one replacement line that corrects the error.
most_freq_vowel("fly")
to determine what is returned (or, if the function does not return, describe the error that occurs),
providing the same details required in part (a).
Now write the output that should have been returned for that input if the function was working
correctly.
None
Finally, identify a minimal fix for this error, by giving exactly one line number in the code that is
to change, and then providing one replacement line that corrects the error.
Describe what the result of that function call should be, and what it would actually be as a result of
this error.
Finally, identify a minimal fix for this error, by giving exactly one line number in the code that is
to change, and then providing one replacement line that corrects the error.
(d) [3 marks] Now locate the other error in the function, and briefly describe it in one sentence.
You do not need to give a function call that would expose this error.
Now identify a minimal fix for this error, by giving exactly one line number in the code that is to
change, and then providing one replacement line that corrects the error.
Recall the Matching Game from Assignment 1, which featured a number of colored pieces on
a two-dimensional board, with the board represented in Python by a list of lists. Each piece was
represented as a string containing a single upper-case character between ’A’ and ’Y’. The character
’Z’ was used to indicate a blank position on the board. For example, a board might have four
columns and three rows and look like this:
In this question you will implement a number of “powerups” – functions that manipulate the board
in ways that would not normally be permitted by the rules of the game. You are not required to
include comments or docstrings in your functions, but may if you wish.
row_destroyer(board, row)
that takes two parameters: board, a list of lists representing a game board; and row, the index
position of a row on the board. You may assume that row has a valid value for the given board.
Your function should alter the board so that all of the pieces in the row specified by row are replaced
by blanks (that is, the character ’Z’).
For example:
>>> board = [[’B’, ’G’, ’B’], [’G’, ’B’, ’Y’], [’G’, ’G’, ’Y’]]
>>> row_destroyer(board, 1)
>>> print(board)
[[’B’, ’G’, ’B’], [’Z’, ’Z’, ’Z’], [’G’, ’G’, ’Y’]]
EMPTY_PIECE = ’Z’
def row_destroyer(board, row):
board[row] = [EMPTY_PIECE]*len(board[row])
piece_destroyer(board, piece)
that takes two parameters: board, a list of lists representing a game board; and piece, a single-
character string representing a piece.
Your function should alter the board so that all pieces that have the same color as piece are replaced
by blanks (that is, the character ’Z’).
For example:
>>> board = [[’B’, ’G’, ’B’], [’G’, ’B’, ’Y’], [’G’, ’G’, ’Y’]]
>>> piece_destroyer(board, ’B’)
>>> print(board)
[[’Z’, ’G’, ’Z’], [’G’, ’Z’, ’Y’], [’G’, ’G’, ’Y’]]
EMPTY_PIECE = ’Z’
def piece_destroyer(board, piece):
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] == piece:
board[row][col] = EMPTY_PIECE
put_in_order(board)
that takes one parameter: board, a list of lists representing a game board.
Your function should rearrange the pieces on the board so that they are placed in alphabetically
sorted order starting from the lowest index position.
Here are two possible execution sequences:
>>> board = [[’A’, ’C’, ’E’], [’D’, ’F’, ’H’], [’B’, ’G’, ’I’]]
>>> put_in_order(board)
>>> print(board)
[[’A’, ’B’, ’C’], [’D’, ’E’, ’F’], [’G’, ’H’, ’I’]]
>>> board = [[’B’, ’G’, ’B’], [’G’, ’B’, ’Y’], [’G’, ’G’, ’Y’]]
>>> put_in_order(board)
>>> print(board)
[[’B’, ’B’, ’B’], [’G’, ’G’, ’G’], [’G’, ’Y’, ’Y’]]
def put_in_order(board):
pieces = []
for row in board:
pieces = pieces + row
pieces.sort()
that takes three parameters: board, a list of lists representing a game board; row, an index value
for a row in the board; and col, an index value for a column in the board. You may assume that
row and col have valid values for the given board.
Your function should modify the board such that the piece at row, col is replaced by a blank (’Z’).
Furthermore, any piece that is immediately above, below, left or right of a piece that was replaced
and has the same color as the piece that was replaced should also be replaced by a blank.
This process should continue until no more replacements are possible.
Here are three possible execution sequences:
>>> board = [[’B’, ’B’, ’A’], [’B’, ’A’, ’A’], [’A’, ’A’, ’B’]]
>>> connected_destroyer(board, 0, 0)
>>> print(board)
[[’Z’, ’Z’, ’A’], [’Z’, ’A’, ’A’], [’A’, ’A’, ’B’]]
>>> board = [[’B’, ’B’, ’A’], [’B’, ’B’, ’A’], [’A’, ’A’, ’B’]]
>>> connected_destroyer(board, 1, 1)
>>> print(board)
[[’Z’, ’Z’, ’A’], [’Z’, ’Z’, ’A’], [’A’, ’A’, ’B’]]
>>> board = [[’B’, ’B’, ’B’], [’B’, ’A’, ’B’], [’B’, ’B’, ’A’]]
>>> connected_destroyer(board, 0, 0)
>>> print(board)
[[’Z’, ’Z’, ’Z’], [’Z’, ’A’, ’Z’], [’Z’, ’Z’, ’A’]]
EMPTY_PIECE = ’Z’
def connected_destroyer(board, row, col):
piece = board[row][col]
board[row][col] = EMPTY_PIECE
if row + 1 < len(board) and board[row + 1][col] == piece:
connected_destroyer(board, row + 1, col)
if row - 1 >= 0 and board[row - 1][col] == piece:
connected_destroyer(board, row - 1, col)
if col + 1 < len(board[row]) and board[row][col + 1] == piece:
connected_destroyer(board, row, col + 1)
if col - 1 >= 0 and board[row][col - 1] == piece:
connected_destroyer(board, row, col - 1)