-7

Given a list like N = [10,7,6,5,4,3,2,1] and PooledMoney=23

I need to print all lists like given below

[10,7,6]
[10,7,5,1]
[10,7,3,2,1]
[10,6,5,2]
[7,6,5,3,2]

This is my attempt:

for L in range(0,len(N)+1):
    for subset in itertools.combinations(N,L):
        #print subset
        for i in subset:
            sum = sum + i
        if(sum==PooledMoney):
            print subset`

This doesn't print the correct subsets.

Gokul1794
  • 137
  • 11

1 Answers1

0

You were pretty close:

import itertools

l = [1,2,3,4,5,6]
ts = 8

def psl(l, ts):
    for i in range(len(l)):
        for c in itertools.combinations(l, i):
            if sum(c) == ts:
                print list(c)

psl(l, ts)
HeinzKurt
  • 612
  • 9
  • 18