Skip to content

Instantly share code, notes, and snippets.

@ZeroPivot
Last active October 18, 2017 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZeroPivot/98478f7d9081d240bf1a62b80bb4e34b to your computer and use it in GitHub Desktop.
Save ZeroPivot/98478f7d9081d240bf1a62b80bb4e34b to your computer and use it in GitHub Desktop.
Powerset
def powerset(array)
a=array
powerset=[]
(a.length).times do |num|
powerset << a.combination(num+1).to_a
end
powerset.flatten(1)
end
p powerset(["a", "b","c"])
#=> [["a"], ["b"], ["c"], ["a", "b"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]
@ZeroPivot
Copy link
Author

ZeroPivot commented Oct 18, 2017

Does not include the empty set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment