Created
July 22, 2010 04:37
-
-
Save lynxluna/485576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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