300 views
1 1 vote

Consider the following function:

def mystery(n):
    if n <= 0:
        return 1
    else:
        return mystery(n - 1) + mystery(n - 2)

If the function is called as $\texttt{mystery(4)}$, how many total function calls are made (including the initial call)?

  1. $15$  
     
  2. $7$
     
  3. $17$
     
  4. $9$

3 Answers

2 2 votes

Assume that \( C(n) \) is the total number of function calls made when \( \texttt{mystery}(n) \) is called (including the initial call).

For \( n > 0 \), the function counts the current call → 1, then calls \( \texttt{mystery}(n-1) \), and calls \( \texttt{mystery}(n-2) \).

So, the recurrence becomes:     \( C(n) = 1 + C(n-1) + C(n-2) \)

Now, notice the base case: when \( n \le 0 \), it simply returns 1. Hence, \( C(n) = 1 \quad \text{for } n \le 0 \).

\[ C(0) = 1, \quad C(-1) = 1 \]

Let us fill in the entries in the following table:

\[ \begin{array}{|c|c|c|c|c|c|} \hline n & -1 & 0 & 1 & 2 & 3 & 4 \\ \hline C(n) & & & & & & \\ \hline \end{array} \]

Filling in the base cases first:

\[ \begin{array}{|c|c|c|c|c|c|} \hline n & -1 & 0 & 1 & 2 & 3 & 4 \\ \hline C(n) & 1 & 1 & & & & \\ \hline \end{array} \]

Now, filling in the remaining entries. Each entry is the sum of the previous two entries plus 1:

\[ \begin{array}{|c|c|c|c|c|c|} \hline \color{#0d47a1}{n} & -1 & 0 & 1 & 2 & 3 & 4 \\ \hline \color{#0d47a1}{C(n)} & 1 & 1 & 3 & 5 & 9 & \color{#d32f2f}{\boxed{15}} \\ \hline \end{array} \]

Correct Option: A

If they ask for \( \texttt{mystery}(10) \), you can simply extend the table:

\[ \begin{array}{|c|c|c|c|c|c|c|c|c|c|c|c|} \hline n & -1 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ \hline C(n) & 1 & 1 & 3 & 5 & 9 & 15 & 25 & 41 & 67 & 109 & 177 & 287 \\ \hline \end{array} \]

Also, I noticed that some students made a silly mistake and read the base case in the function as \( \texttt{n} \le 1 \) instead of \( \texttt{n} \le 0 \). If you make this mistake, the answer will be 9, because the base cases would then be \( C(0) = 1 \) and \( C(1) = 1 \).

\[ \begin{array}{|c|c|c|c|c|c|} \hline n & 0 & 1 & 2 & 3 & 4 \\ \hline C(n) & 1 & 1 & 3 & 5 & 9 \\ \hline \end{array} \]

0 0 votes

Step 1: Trace the function calls for mystery(4)

 

The function mystery(4) calls mystery(3) and mystery(2). 

We can represent this as a call tree.

Each node in the tree is a function call.

 

Step 2: Expand the call tree

 

The recursive calls continue until the base case n &lt;= 0 is reached, at which point the function returns 1 without further recursion.

  • mystery(4) calls mystery(3) and mystery(2)
  • mystery(3) calls mystery(2) and mystery(1)
  • mystery(2) calls mystery(1) and mystery(0)
  • mystery(1) calls mystery(0) and mystery(-1)
  • mystery(0) returns 1 (base case)
  • mystery(-1) returns 1 (base case)

Step 3: Count all function calls

 

We can visualize all calls in a tree structure:

  • mystery(4) (1 call)
    • mystery(3) (1 call)
      • mystery(2) (1 call)
        • mystery(1) (1 call)
          • mystery(0) (1 call)
          • mystery(-1) (1 call)
        • mystery(0) (1 call)
      • mystery(1) (1 call)
        • mystery(0) (1 call)
        • mystery(-1) (1 call)
    • mystery(2) (1 call)
      • mystery(1) (1 call)
        • mystery(0) (1 call)
        • mystery(-1) (1 call)
      • mystery(0) (1 call)

Summing all the calls: $1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 15$ calls.

 

Answer:  The correct option is (A) 15.

Answer:
Position:
Show:

Related questions

1 1 vote
0 0 answers
235
235 views
GO Classes asked Feb 17
235 views
Let $G=(V,E)$ be a directed graph, and let $G^R$ denote the graph obtained by reversing all the edges of $G$.Which of the following statements is/are TRUE?If a vertex $v$...
0 0 votes
1 1 answer
237
237 views
GO Classes asked Feb 17
237 views
Consider the following Python code:def outer(): x = [] def inner(val): x.append(val) return x return innerNow consider:f1 = outer() f2 = outer...
1 1 vote
2 2 answers
333
333 views
GO Classes asked Feb 17
333 views
Consider the following function:def fun(L, i = 0): if i >= len(L) - 1: return 0 if L[i] L[i + 1]: L[i], L[i + 1] = L[i + 1], L[i] return ...
1 1 vote
1 1 answer
314
314 views
GO Classes asked Feb 17
314 views
Consider the following Python code:def append_to_lst(val, lst=[]): lst.append(val) return lst print(append_to_lst(1)) print(append_to_lst(2)) print(append_to_lst(...