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