Question 1: Trace the following recursive function when trace_practice (5) is ca
ID: 3909826 • Letter: Q
Question
Question 1: Trace the following recursive function when trace_practice (5) is called, draw the tracing tree and write down the output. What is the base case of this recursion? def trace practice (n) if (n > 0) print("before " , n) trace_practice (n-2); trace_practice (n-3) print("after ", n) Question 2: Trace the following recursive function when rec_func (5) is called, and find the output by drawing the tracing tree def rec_func (n): if (n5) else: return result result-n/2 resultn-2 result n rec func (n-2)rec func (n+5)Explanation / Answer
1) Function flow sequence:
trace_pratice(5) ------>trace_practice(3)---------->trace_practice(1).
The function will recrsively call itself from the statement trace_practice(n-2) until n < 0.
OUTPUT:
before 5
before 3
before 1.
2)
rec_func(5)---------->rec_func(10).
The flow of execution of a statement is from right to left therefore the rightmost function will be executed first(rec_func(n + 5)).therefore value of n will become 10.
OUTPUT:
result = 7
7 will be returned from the function.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.