Skip to content

Instantly share code, notes, and snippets.

@aergonaut
Last active December 14, 2015 06:49
Show Gist options
  • Save aergonaut/5045715 to your computer and use it in GitHub Desktop.
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
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