Skip to content

Instantly share code, notes, and snippets.

@topher6345
Created July 9, 2016 02:03
Show Gist options
  • Save topher6345/888adb3c18b2527ad76eba089484baf7 to your computer and use it in GitHub Desktop.
Save topher6345/888adb3c18b2527ad76eba089484baf7 to your computer and use it in GitHub Desktop.
Power Set in Ruby
require 'set'
class Set
def power_set
make_permutation = proc { |n| self.to_a.permutation(n).to_a.map(&:to_set).to_set}
Set.new([Set.new([]),(1..self.size).map(&make_permutation)])
end
end
Set.new([1,2,3]).power_set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment