Last active
December 14, 2015 06:49
-
-
Save aergonaut/5045715 to your computer and use it in GitHub Desktop.
Given a set S, generates the power set P(S) by using a mapping of the elements in S to binary strings
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 pset(set) | |
bins = (0...(2 ** set.length)).map { |x| ("%0#{set.length}d" % x.to_s(2)).split('').map(&:to_i) } | |
bins.map do |a| | |
a.map.with_index.map { |v, i| v > 0 ? set[i] : nil }.compact | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment