A data-import program checks whether two lists contain the same number of entries, regardless of their contents. A base case returns a result without making another recursive call.
In the function below, tail(x) removes the first entry from a nonempty list.
def same_length(a, b): if not a and not b: return "same" if not a or not b: return "different" return same_length(tail(a), tail(b))
Complete the table with the exact returned word, either same or different, for each pair of initial list lengths. Include the result of the entire function call, rather than the branch first reached.
Unlock full course notes by signing up
Maximum number of merge applications
The function below merges two lists of numbers, each sorted in increasing order.
Suppose lon1 has 7 elements and lon2 has 5 elements, all values distinct. Over all possible such sorted lists, what is the largest number of applications of merge that a single initial call can produce, counting the initial application? Give your answer as a whole number.
Recovering a price after pair alignment changes
An invoice test program uses simultaneous recursion, organizing one recursive computation around two list inputs. Each quantity is paired with the unit price at the same position. The helper head(x) returns the first entry, and tail(x) removes that entry.
The quantity list is [4, 2, 5], measured in items, and the original price list is [3, 6, 2, 8], measured in dollars per item. The function excludes entries remaining after either list becomes empty. Assume there are no taxes or other charges.
Find both quantities below: