Problem 2: Tracing list comprehensions and recursion 50 points; individual-only
ID: 3855985 • Letter: P
Question
Problem 2: Tracing list comprehensions and recursion
50 points; individual-only
Put your answers for this problem in a plain-text file named ps4pr2.txt.
1.Consider the following Python program:
Trace the execution of this program. Your answer should include:
a table for the variables in the global scope that illustrates how their values change over time. You can begin it as follows:
one or more separate tables that illustrate the changes in the local variables that belong to the function; you can either use a different table for each function call, or one table for the function as a whole.
Your table(s) for the function should have the following columns:
In the column for lc, you should show the how the result of the list comprehension is gradually built up. In other words, for each new value of the list comprehension’s “runner” variable, you should show the current partial result of the list comprehension.
We included a similar table for a method called myst as part of the solution to a clicker question in the lecture notes on Recursive Design.
a separate section that specifies the output of the program (i.e., the values that are printed).
2: Consider the following Python function:
Trace the execution of the following call to this function. e.g. mystery2([-2, 1, -5, 4])
Your answer should include:
a table that illustrates the changes in the values of x and scored_vals as the result of the list comprehension is gradually built up. In other words, for each new value of the list comprehension’s “runner” variable, you should show the current partial result of the list comprehension.
the value assigned to the variable bestpair
the return value of the function call
3. Briefly describe what the function mystery2 (from the previous part of this problem) does in general. In other words, for an arbitrary list of numbers vals, what will mystery2 return?
4.Consider the following recursive function:
Trace the execution of the following call to this function. e.g. mystery4('banana')
Your answer should illustrate the sequence of function calls that are made – from the initial call to the base case. Include a separate section for each call, taking an approach that is similar to the one we have often used in lecture. Begin each section with the call itself (e.g., `mystery4(‘banana’)). Then include a line that explicitly states the value assigned to the parameter. Next, if the call is a base case, you can just show the value that is returned. If the call is a recursive case, you should show the recursive call and its result, along with the value that is ultimately returned. We also encourage you to use indenting to emphasize the way in which one call occurs in the context of prior calls.
For example, recall the num_vowels function from lecture. When tracing the call num_vowels('ate'), you would start by getting all the way down to the base case:
Note that we have left blank lines between sections. Then, once you have reached the base case, you can go back and add in both the results of the recursive calls and the values returned by the earlier calls, using the space provided by the blank lines:
Take this same approach when tracing mystery4('banana').
5: What is the final result of the call mystery4('banana')?
Explanation / Answer
The answer is as follows:
1. The value changes for variables is as follows:
x y lc
1 5 4 [-1,1,3,5]
2 5 8 [-1,1,3,5]
print(x,y) will print 5 8
3. 5 8 [-1,1,3,5,7]
15 8 [-1,1,3,5,7]
print(x,y) will print 15 8
2. mystery2([-2, 1, -5, 4])
scored_vals = [[4,-2],[1,1],[25,-5],[16,4]]
best_pair = [16,4]
function mystery2 will return 4
3. mystery2 is in genral giving the maximum value in the list
4. mystery4("banana")
mystery4("banana") (returned value = "nnb")
mystery4("anana") (returned value = "nn")
myster4("nana") (returned value = "nn")
mystery4("ana") (returned value = "n")
mystery4("na") (returned value = "n")
mystery4("a") (returned value = "")
5. Final result is "nnb". It seems that mystery4 is reversing the given string while removing the repeated characters.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.