Recognizing simultaneous exhaustion

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.


Python
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.

First list lengthSecond list lengthReturned word
00
04
30
33
25

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Simultaneous Recursion Questions: