(Python) The following recursive function has a logical error. What can you offer to fix it and produce the correct result?
def myFunction(a):
""" pre: a is a list of integer numbers, len(a) is even
number
post: returns the list of the sums of the last and first,
second and second to last, thrid and thrid to last, ...
Example: for the list [1,4,3,7,5,6]
the function returns the list [7,9,10],
i.e. 1+6 = 7, 4+5 = 9, 3+7 - 10"""
r = []
return helper(a,r,0,len(a)-1)
def helper(a, result, left, right):
"""pre: a is the original list, result is the resulting list,
left is the index of the element to the left
for the sum to be computed, and right
is the index of the element to the right."""
s = a[left] + a[right]
result.append(s)
helper(a, result, left+1, right-1)
return result
def myFunction(a): """ pre: a is a list of integer numbers, len(a) is even number post: returns the list of the sums of the last and first, second and second to last, thrid and thrid to last, ... Example: for the list [1,4,3,7,5,6] the function returns the list [7,9,10], i.e. 1+6 = 7, 4+5 = 9, 3+7 - 10""" r = [] return helper(a, r, 0, len(a) - 1) def helper(a, result, left, right): """pre: a is the original list, result is the resulting list, left is the index of the element to the left for the sum to be computed, and right is the index of the element to the right.""" if left >= right: return [] s = a[left] + a[right] result.append(s) helper(a, result, left + 1, right - 1) return result
(Python) The following recursive function has a logical error. What can you offer to fix it...