fork download
  1.  
  2. from itertools import product, compress
  3.  
  4. def sub_sum(L, goal):
  5. return [ tuple(compress(L,f)) for f in product([0, 1], repeat=len(L))
  6. if sum(compress(L,f)) == goal ]
  7.  
  8. L = [ 25, 30, 15, 20, 30, 40, 35, 10 ]
  9. R = sub_sum(L, 120)
  10.  
  11. for item in R: print(item)
  12.  
Success #stdin #stdout 0.01s 9992KB
stdin
Standard input is empty
stdout
(15, 30, 40, 35)
(15, 20, 40, 35, 10)
(30, 20, 30, 40)
(30, 15, 40, 35)
(30, 15, 30, 35, 10)
(25, 20, 40, 35)
(25, 20, 30, 35, 10)
(25, 15, 30, 40, 10)
(25, 30, 30, 35)
(25, 30, 20, 35, 10)
(25, 30, 15, 40, 10)
(25, 30, 15, 20, 30)