Home » programming » Subset sum

Subset sum


Subset sum as a decision problem in Python.

def bruteforce(x_list, target):
    for x in almost_powerset(x_list):
    	if sum(x) == target:
    		return True
    return False

def almost_powerset(iterable):
    '''powerset([1,2,3]) --> (1,2) (1,3) (2,3) (1,2,3)

    s = list(iterable)
    return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(2, len(s) + 1))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: