Skip to content

Instantly share code, notes, and snippets.

@DomNomNom
Last active April 5, 2016 21:48
Show Gist options
  • Save DomNomNom/a34d88ef1e672cd9b760 to your computer and use it in GitHub Desktop.
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.
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