I need to create a recursive function that adds the numbers of any given array, then removes the first element, then adds the array and do this until the array only has element left. my function does this but at the moment I can only see the addition by putting a puts statement but also need the results as the return value like this =>[20,20,19,16,10] and dont know how to go about this as its putting the results separately. Thanks for your help. The function needs to do this recursively: I this is my code:
def parts_sums(ls)
results_arr=[]
if( ls.length === 1)
return ls
end
p results =ls.sum
parts_sums(ls.drop(1))
p results_arr << results
end
parts_sums([0, 1, 3, 6, 10])
# ls = [0, 1, 3, 6, 10].sum =>20
# ls = [1, 3, 6, 10].sum => 20
# ls = [3, 6, 10].sum =>19
# ls = [6, 10].sum =>16
# ls = [10]=>10