2021 Fall Final
2021 Fall Final
This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before
starting. Budget your time wisely. Use the back of the pages if you need more space. You may tear
the pages apart; we have a stapler at the front of the room.
It is a violation of the Academic Integrity Code to look at any exam other than your
own, look at any reference material, or otherwise give or receive unauthorized help.
You will be expected to write Python code on this exam. We recommend that you draw vertical
lines to make your indentation clear, as follows:
def foo():
if something:
do something
do more things
do something last
Unless you are explicitly directed otherwise, you may use anything you have learned in this course.
You may use the backside of each page for extra room for your answers. However, if you do this,
please indicate clearly on the page of the associated problem.
References
String Operations
Expression Description
List Operations
Expression Description
Dictionary Operations
Expression Description
Page 2
Last Name: First: Netid:
(b) [9 points] Consider the functions listed below. Note that ValueError and AssertionError
are both subclasses of Exception but neither is a subclass of the other. On the next page,
we want you to write down the text printed out (e.g. the traces) for each function call.
21 def third(n):
22 print('Start third')
23 if n == 0:
24 print('Third at n == 0')
25 raise ValueError('Value error')
26 elif n == 1:
27 print('Third at n == 1')
28 assert False, 'Assertion error'
29 print('End third at '+str(n))
30 return n
Page 3
Last Name: First: Netid:
ii. first(1)
(c) [4 points] Name four sorting algorithms we covered in class, and the running time of each
(e.g. n, n log n, or n2 ).
Page 4
Last Name: First: Netid:
Note that 1x8 matrix is not a 1-dimensional list. It is a nested list containing one list (the
row) of eight elements. Similarly, an 8x1 matrix is a nested list of eight one-element lists.
When you reshape the matrix, you process the elements in normal row-major order. So top row
first, left-to-right. With this in mind, implement the function below according to its specifica-
tion. You are allowed to use any form of iteration (for-loop or while-loop) that you want.
Hint: This function is a lot simpler if you first create a 1-dimensional list with the elements in
the proper order. Then use this new list to make your matrix.
def reshape(table,r,c):
"""Returns a copy of the table reshaped to have r rows and c cols
Page 5
Last Name: First: Netid:
List nums is sorted (e.g. the elements are in order). The function puts
puts x into nums at the right position so it is still ordered. If x is
already in nums, this function still inserts a copy of x.
Page 6
Last Name: First: Netid:
The cursor itself will be a solid black GRectangle. As a reminder, the GRectangle class comes
with the following (mutable) attributes.
Attribute Invariant Description
x float x-coordinate of the center of the rectangle.
y float y-coordinate of the center of the rectangle.
left float < x x-coordinate of the left edge of the rectangle.
right float > x x-coordinate of the right edge of the rectangle.
top float > y y-coordinate of the top edge of the rectangle.
bottom float < y y-coordinate of the bottom edge of the rectangle.
width float > 0 The width along the horizontal axis.
height float > 0 The height along the vertical axis.
fillcolor str The interior color (represented as the name, e.g. 'blue').
There are other attributes, but they can be ignored for this problem. Recall that when you
create a GRectangle, you use keyword arguments to specify the attributes, and all arguments
are optional (e.g. GRectangle(fillcolor='red')).
The class GLabel is a subclass of GRectangle and has additional attributes. You can ignore
all the font attributes for this problem. We will assume the default font. Instead, you
only need to use the attribute text, which is a string. In addition, the version of GLabel we
are using has the following methods.
Method Precondition Description
l.descent() None Returns: The y-coord of the lowest character position.
l.ascent() None Returns: The y-coord of the highest character position.
l.leftside(p) an index of l.text Returns: The x-coord left of character at position p.
l.rightside(p) an index of l.text Returns: The x-coord right of character at position p.
To greatly simplify this problem, we are going to
assume that text is an immutable attribute of the leftside(0) ascent()
GLabel, meaning it does not change after you set it
in the constructor call. Otherwise, it will be very hard
to satisfy all of the invariants (because they depend on
Hello
the length of text). We will also assume all the text
descent() rightside(4)
fits on one line (e.g. newlines are ignored). Once the
rightside(1) leftside(2)
attribute text is set, you can call the methods above
to get the position of the characters in the label.
You will note that this diagram includes some spacing between the actual characters and the
values returned, such as with l.descent(). This is natural – it is the way that fonts work.
With this in mind, implement GTextField as outlined on the next two pages. This is a subclass
of GLabel that has a single GRectangle as an attribute (for the cursor). We have provided the
specifications for the methods __init__, update, and draw. You should also add getters and
setters (where appropriate) for the new attributes. Those setters must have preconditions to
enforce the attribute invariants. Furthermore, all methods (not just the setters) must enforce
the preconditions for any value other than self. Finally, all type-based preconditions should
be enforced with isinstance and not type.
You do not need setters and/or getters for the attributes inherited from GRectangle or GLabel.
Those can be left as is. Remember that attribute text is immutable.
Page 7
Last Name: First: Netid:
class GTextField(GLabel):
"""A class representing a label with a flashing cursor
The cursor is drawn as a rectangle that is 2 pixels wide, and whose top
is ascent() and bottom is descent(). If p is the current position, the
horizontal coordinates of the cursor are defined as follows:
* if p is 0, the right edge of the cursor is leftside(0)
* if p is len(text), the left edge of the cursor is rightside(p-1)
* otherwise the cursor is centered between leftside(p) and rightside(p-1)
The cursor is only drawn if the visible attribute is True. The visible
switches between True and False every BLINK_RATE animation frames.
Page 8
Last Name: First: Netid:
The cursor starts off visible and at position len(text) (after the last
character). This method sets the frame counter for blinking to BLINK_RATE.
Page 9
Last Name: First: Netid:
At the top of the next page, we have provided the contents of global space and the heap
after executing the assignment statement to b on line 15 above. We want you to diagram the
assignment statement on the next line, line 16. You should draw a new diagram every time
a call frame is added or erased, or an instruction counter changes. There are a total of nine
diagrams to draw, not including the initial diagram we have provided. You may write unchanged
in any of the three spaces if the space does not change at that step.
Page 10
Last Name: First: Netid:
Page 11
Last Name: First: Netid:
Page 12