Last active
October 18, 2017 14:24
-
-
Save ZeroPivot/98478f7d9081d240bf1a62b80bb4e34b to your computer and use it in GitHub Desktop.
Powerset
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
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"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not include the empty set