Created
December 4, 2014 11:17
-
-
Save samarthbhargav/74b08189a5d1203b845e to your computer and use it in GitHub Desktop.
Recursive generation of a power set
This file contains hidden or 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
# as seen in 6.00.2x, Week 6, Lecture 1, Video 4 | |
def powerset(iterable): | |
if len(iterable) == 0: | |
return [[]] | |
smaller = powerset(iterable[1:]) | |
withElem = [] | |
for s in smaller: | |
withElem.append( s + [iterable[0]]) | |
return withElem + smaller | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment