Skip to content

Instantly share code, notes, and snippets.

@damzam
Created June 13, 2015 03:39
Show Gist options
  • Save damzam/ca64b897a4cb9f818003 to your computer and use it in GitHub Desktop.
Save damzam/ca64b897a4cb9f818003 to your computer and use it in GitHub Desktop.
Create a superset efficiently in Python
#!/usr/bin/env python
"""
Create a superset from the integers provided
e.g. ./superset.py 1 2 3
[set([]), set([1]), set([2]), set([3]), set([1, 2]),
set([1, 3]), set([2, 3]), set([1, 2, 3])]
"""
import sys
def main(*numbers):
sets = [set([])]
for n in numbers:
sets.extend([s | {n} for s in sets])
print sets
if __name__ == '__main__':
numbers = [int(i) for i in sys.argv[1:]]
main(*numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment