Skip to content

Instantly share code, notes, and snippets.

@epitron
Created March 17, 2010 22:12
Show Gist options
  • Save epitron/335789 to your computer and use it in GitHub Desktop.
Save epitron/335789 to your computer and use it in GitHub Desktop.
#!/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