Last active
April 5, 2016 21:48
-
-
Save DomNomNom/a34d88ef1e672cd9b760 to your computer and use it in GitHub Desktop.
Two ways of writing powerset which produce the same ordering. Have a go at proving it.
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
def powerset(list): | |
if not list: | |
yield set() | |
return | |
*rest, last = list | |
yield from powerset(rest) | |
for subset in powerset(rest): | |
yield subset | {last} | |
def powerset2(list): | |
if not list: | |
yield set() | |
return | |
first, *rest = list | |
for subset in powerset2(rest): | |
yield subset | |
yield subset | {first} | |
numbers = [2, 5, 9] | |
print (list(powerset(numbers))) | |
print (list(powerset2(numbers))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment