Language/Type: Python lists Marty Stepp (on 07/08) Author: Consider the followin
ID: 3600488 • Letter: L
Question
Language/Type: Python lists Marty Stepp (on 07/08) Author: Consider the following function: def 1ist,mystery2(a): for i in range(1, len(a) 1) Indicate in the right-hand column what values would be stored in the list after the function list mystery executes if each integer list below is passed as a parameter to it. a1 [42, 42] list_nystery2(a1) Sound F/X 1ist mystery2(a2) a3 = [7, 7, 3, 8, 2] list.mystery2(a3) 24 = [4, 2, 3, 1, 2, 5] list mystery2(a4) a5 [6, 8, -1, 3, 5, 8. -31 list.mystery2(a5) SubmitExplanation / Answer
def list_mystery2(a): #Takes a list a as input.
for i in range(1, len(a)-1): #Starting from the second element in the list, and moving till the penultimate(last but one) element.
a[i] = a[i-1] - a[i] + a[i+1] #Just update the element which is the sum of the two neighboring elements subtracted from this element.
return a #If this is not returned, the function returns None.
a1 = [42, 42] #There are no elements in between. So, the list remains intact.
#Output is: [42, 42].
a2 = [6, 2, 4] #Only one element will be modified. at index 1 (6 + 4 - 2 = 8).
#Output is: [6, 8, 4]
a3 = [7, 7, 3, 8, 2] # Three elements will be modified.
# a3[1] = 7 + 3 - 7 = 3.
# a3[2] = 3 + 8 - 3 = 8.
# a3[3] = 8 + 2 - 8 = 2.
#Output is: [7, 3, 8, 2, 2]
a4 = [4, 2, 3, 1, 2, 5] #Four elements will be modified.
# a4[1] = 4 + 3 - 2 = 5.
# a4[2] = 5 + 1 - 3 = 3.
# a4[3] = 3 + 2 - 1 = 4.
# a4[4] = 4 + 5 - 2 = 7.
#Output is: [4, 5, 3, 4, 7, 5]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.