Question

(Python) The following recursive function has a logical error. What can you offer to fix it...

(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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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




Add a comment
Know the answer?
Add Answer to:
(Python) The following recursive function has a logical error. What can you offer to fix it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT