Home > OS >  What to put in blanks to complete python program (recursion)
What to put in blanks to complete python program (recursion)

Time:05-23

I've been at it for hours now yet I still don't know what to put in these blanks. I have an idea on how it will work but I don't know how to apply it. Help!

Basically, I need to find out what are the blanks in order for the program to run. It's a recursion problem where the program would just basically get the sum of all the elements in the list.

enter image description here

CodePudding user response:

Without having to fill in the blanks:

def listsum(numlist):
  size=len(numlist)
  if(size==1): return numlist[0]
  else: 
    mid=size//2
    return listsum(numlist[:mid]) listsum(numlist[mid:])

numbers=[3, 5, 4, 1, 7, 2, 9, 8, 0, 6]

Having to fill in the blanks:

def listsum(numlist, size):
  if size==0:
    return 0
  elif size==1:
    return numlist[0]

  mid=size//2

  return listsum(numlist[:mid], mid) listsum(numlist[mid:], size-mid)

numbers=[3, 5, 4, 1, 7, 2, 9, 8, 0, 6]
print(listsum(numbers, len(numbers)))

I highly question why such a piece of homework would be meted out to students

  • Related