Question
Consider the following recursive function: def mystery2(s): takes a string s and does something with it if len(s) Trace the execution of the
-
Consider the following recursive function:
def mystery2(s): """ takes a string s and does something with it """ if len(s)
Trace the execution of the following call to this function.
mystery2('bonbon')
To do so, complete the template that we have provided in section 1-4 section of ps3pr1. In particular, you should:
-
Fill in a separate frame for each call. We have filled in some of the components of the first call for you, and we have given you templates for up to five additional calls.
You may not need all of the provided frames.
-
You should replace each ... with the appropriate string. In particular, the frame for each call should begin a line that explicitly states the value assigned to the parameter, as we have done for the first two calls.
-
Next, if the call is a base case, you should:
-
Replace the entire line for result_rest with the words base case.
-
Replace the ... after the word return with the value that is returned.
-
-
If the call is a recursive case, you should fill in the value that is being passed into the recursive call on the line for result_rest (as we have done for the first call).
-
Once you have reached the base case, you should work your way back through the sections for the previous calls. Fill in both the results of the recursive call (i.e, the value assigned to result_rest) and the value returned by the call itself.
We took a similar approach to tracing in Task 1 of Lab 3 and Lab 4, and in Problem 2 of PS 2.
-
-
What is the final result of the call mystery2('bonbon')? Put your answer in the box provided.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started