Skip to content

Instantly share code, notes, and snippets.

@jg-you jg-you/supersets.py
Last active Aug 29, 2015

Embed
What would you like to do?
Find all supersets of a collection
collection = [{1, 2, 3, 4, 5},
{1, 2},
{1, 2, 3, 4, 5, 6},
{3, 4, 8},
{3, 4, 11},
{3},
{3},
{12},
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 7, 9},
{1, 2, 3, 4, 5, 8},
{3, 5, 10}]
sizes = set()
for i in collection:
sizes.add(len(i))
sizes = sorted(sizes, reverse=True)
X = dict()
for s in sizes:
X[s] = []
for s in collection:
X[len(s)].append(s)
for reference_size in sizes:
for reference_set in X[reference_size]:
for s in sizes:
if s == reference_size:
X[s][:] = [x for x in X[s] if not x.issubset(reference_set)] +\
[reference_set]
if s < reference_size:
X[s][:] = [x for x in X[s] if not x.issubset(reference_set)]
updated_collection = []
for s in X:
updated_collection += X[s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.