Created
March 17, 2010 22:12
-
-
Save epitron/335789 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby1.9 | |
class Array | |
def powerset | |
# the bit pattern of the numbers from 0...2^(num_elements) can be used to select the elements of the set... | |
(0...2**size).map do |bitmask| | |
select.with_index{ |e, i| bitmask[i] == 1 } | |
end | |
end | |
end | |
p [1,2,3].powerset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment