fork download
  1. arr = [1, 2, 2, 3, 5]
  2. target = 8
  3.  
  4. def comb_sum(arr, current_index, target, result, ans):
  5. if target == 0:
  6. print result
  7. ans.append(result)
  8. return 0
  9. if target < 0:
  10. return 1
  11. if current_index == len(arr):
  12. return 1
  13.  
  14. result.append(arr[current_index])
  15. comb_sum(arr, current_index+1, target - arr[current_index], result, ans)
  16. result.pop()
  17.  
  18. comb_sum(arr, current_index+1, target, result, ans)
  19. return ans
  20.  
  21.  
  22.  
  23. print comb_sum(arr, 0, target, [], [])
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
[1, 2, 2, 3]
[1, 2, 5]
[1, 2, 5]
[3, 5]
[[], [], [], []]