fork(2) download
  1. import itertools
  2.  
  3. def yoba(c, n, g):
  4. all_combinations = itertools.combinations_with_replacement(c, n)
  5. combinations = itertools.ifilter(lambda x: sum(x) == g, all_combinations)
  6. return next(combinations, None)
  7.  
  8. c = [1, 2, 5, 10, 25]
  9. g = 30
  10.  
  11.  
  12. print yoba(c, 4, g)
  13. print yoba(c, 1, g)
Success #stdin #stdout 0s 7852KB
stdin
Standard input is empty
stdout
(1, 2, 2, 25)
None