I'm trying to sharpen my noob Python skills by trying a problem my son has in his college CS class. The goal is to create a function that uses recursion to process a list. The function must accept a list of arbitrary length and return a new list in which each element is the sum of itself and the elements to its right. So, if you input the list [5, 3, 2, 4], the function should return [14, 9, 6, 4].
I've written the following code in Python, and it works fine if I put a "print" command in the recursive function to show the final value, but it won't pass its return values. The end result is "None."
Why don't the variables return properly?
def RecursiveProcess(ListIn2, target): #Recursive function that adds to target value the value to its right
if target > -1: #stop function if index is below 0
ListIn2[target] = ListIn2[target] + ListIn2[target+1] #Add value to the right of target to the target value
ListIn2 = RecursiveProcess(ListIn2, target-1) #Call the function again with lower taget value to process the next value
else:
return ListIn2 #return the changed list
def ProcessList(ListIn): #helper function to pass the list and the position of penultimate value
return RecursiveProcess(ListIn, len(ListIn)-2) #return the changed list
print ProcessList([5, 10, 11, 6, 7, 1, 2, 4, 6, 7]) #initiate recursion and print result