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
  7. last=new_list[-1]
  8. new_list.remove(last)
  9. rec = rec_cumsum(new_list)
  10. rec.append(rec[-1]+last)
  11. return rec
  12.  
  13. tests = [[],
  14. [1],
  15. [1,2],
  16. [1,2,3],
  17. [1,2,1,3]]
  18. for t in tests:
  19. print(rec_cumsum(t))
  20.  
  21.  
  22.  
Success #stdin #stdout 0.09s 10840KB
stdin
Standard input is empty
stdout
[]
[1]
[1, 3]
[1, 3, 6]
[2, 3, 4, 7]