Prac Midterm
Prac Midterm
1. [2 + 2 + 8 + 3 marks] Given a list of integers, our friend has written this Python function
to find an index i that satisfies a certain CONDITION (line numbers shown for clarity):
1: def find_index(data):
2: for i, x in enumerate(data):
3: if i == x: return i
4: return -1
2. [3 + 4 + 3 + 5 marks] Recall the btree representation of binary trees from Lab 5. For this
recursive Python function, assume that all nodes in the given btree have int data:
This function is supposed to check if the given tree is actually a binary search tree (BST).
Show that the function is buggy by providing an input argument (tree: btree) as well
as the expected result and the function’s return value for this input argument.
4. [7 + 3 marks] Our friend claims to have written a function to order a list of distinct integers
in such a manner that when these ordered integers are inserted into an initially empty BST
(using the simple insertion algorithm discussed in class), the resulting BST is complete. For
example, given the list [10, 20, 30, 40], our friend’s algorithm:
• should return a valid ordering e.g., [30, 40, 20, 10], but
• should not return an invalid ordering e.g., [30, 40, 10, 20]
5. [3 + 2 marks] Consider the following AI-generated suggestion (Line 3) for this Python
function:
a) Assume that the function argument respects the given type-hint. State one additional
assumption that is being made by the AI-generated code.
b) To see if this additional assumption can be made, ask an appropriate clarifying question
of the form: What should this function return when data = …?