fork download
  1.  
  2. def rec_cumsum(numbers):
  3. ''' Input: numbers - a list of numbers,
  4. Output: a list of cumulative sums of the numbers'''
  5. if len(numbers) <= 1:
  6. return numbers
  7. new_list=numbers
  8. last=new_list[-1]
  9. new_list.remove(last)
  10. rec = rec_cumsum(new_list)
  11. new_list.append(rec[-1]+last)
  12. return last+rec
  13.  
  14. tests = [[],
  15. [1],
  16. [1,2],
  17. [1,2,3],
  18. [1,2,1,3]]
  19. for t in tests:
  20. print(rec_cumsum(t))
  21.  
Runtime error #stdin #stdout 0.09s 10840KB
stdin
Standard input is empty
stdout
[]
[1]