Skip to content

Instantly share code, notes, and snippets.

@lynxluna
Created July 22, 2010 04:37
Show Gist options
  • Save lynxluna/485576 to your computer and use it in GitHub Desktop.
Save lynxluna/485576 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
'''
This code is using binary shift and tricks to create powerset and also generator
'''
def powerset( l ):
''' This code make a powerset list by using grey code'''
# Anggaplah index item itu adalah binary
d = dict(zip(
(1<<i for i in range(len(l))),
(set([e]) for e in l)
))
subset = set()
yield subset
for i in range(1, 1<<len(l)):
subset = subset ^ d[i & -i]
yield subset
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print ''' Usage: powerset <string> '''
sys.exit(1)
print list(powerset(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment