Classes, Encapsulation and Representation Invariants · Predict the output · 3 marks
Write the output of the code below. If the code raises an error, write its type on its own line and stop.
class Shelf:
"""A shelf of books."""
def __init__(self, books: list[str]) -> None:
self._books = books
def __len__(self) -> int:
return len(self._books)
def __contains__(self, title: str) -> bool:
return title in self._books
def __getitem__(self, i: int) -> str:
return self._books[i]
s = Shelf(['Emma', 'Dune', 'Kim'])
print(len(s))
print('Dune' in s, 'dune' in s)
print(s[1], s[-1])
for i in range(len(s)):
print(s[i][0], end='')
print()
print(s[3])3
True False
Dune Kim
EDK
IndexErrors[3] calls __getitem__(3), which indexes a list of length 3.
Stacks, Queues and Exceptions · Write code · 3 marks
Complete the function using one additional Stack and no lists.
def remove_all(s: Stack, item: object) -> None:
"""Remove every occurrence of item from s. The other items remain in
s in their original order.
>>> s = Stack()
>>> for x in [1, 2, 1, 3]:
... s.push(x)
>>> remove_all(s, 1)
>>> s.pop()
3
>>> s.pop()
2
>>> s.is_empty()
True
""" temp = Stack()
while not s.is_empty():
x = s.pop()
if x != item:
temp.push(x)
while not temp.is_empty():
s.push(temp.pop())Linked Lists · Predict the output · 3 marks
Write the output of the code below. “print(lst)” displays a LinkedList like [1 -> 2 -> 3].
lst = LinkedList([3, 1, 4, 1, 5])
prev = lst._first
curr = prev.next
while curr is not None:
if curr.item == 1:
prev.next = curr.next
else:
prev = curr
curr = curr.next
print(lst)
print(prev.item)[3 -> 4 -> 5]
5Each removed node is skipped by prev.next, while prev only advances past nodes that are kept.
Trees · Write code · 2 marks
Implement the method below according to its docstring.
class Tree:
"""A recursive tree.
=== Private Attributes ===
_root: the item at the root, or None if empty
_subtrees: the subtrees of this tree
"""
_root: Any | None
_subtrees: list[Tree]
def count_leaves(self) -> int:
"""Return the number of leaves in this tree.
>>> Tree(None, []).count_leaves()
0
>>> Tree(7, []).count_leaves()
1
>>> t = Tree(1, [Tree(2, [Tree(4, [])]), Tree(3, []), Tree(5, [])])
>>> t.count_leaves()
3
"""def count_leaves(self) -> int:
if self.is_empty():
return 0
elif self._subtrees == []:
return 1
else:
total = 0
for subtree in self._subtrees:
total += subtree.count_leaves()
return totalRecursive Sorting · Predict the output · 3 marks
The following version of mergesort prints a line before and after its recursive work. Assume merge is the course function.
def mergesort(lst: list) -> list:
if len(lst) < 2:
return lst[:]
mid = len(lst) // 2
print('split', lst[:mid], lst[mid:])
left = mergesort(lst[:mid])
right = mergesort(lst[mid:])
result = merge(left, right)
print('merged', result)
return result
mergesort([4, 7, 1, 3, 2])Write the output, in order, one line per print call.
split [4, 7] [1, 3, 2]
split [4] [7]
merged [4, 7]
split [1] [3, 2]
split [3] [2]
merged [2, 3]
merged [1, 2, 3]
merged [1, 2, 3, 4, 7]The left half is always completely sorted (all its splits and merges) before the right half starts.
Elements of Software Design · The University of Texas at Austin (UT Austin, UT, UTexas) · also written CS313E
Make a free, printable C S 313E practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 900 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UT Austin library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh C S 313E practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with The University of Texas at Austin. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.