Sha256: a1191e9a07df0825527e692bea1c9e8fbd9c087bd97f59503392707592576992
Contents?: true
Size: 1.02 KB
Versions: 1
Compression:
Stored size: 1.02 KB
Contents
module Combinatorics module PowerSet module Mixin # # Calculates the power-set of an Enumerable object. # # @return [Array] # The power set. # # @example Power-set of an Array. # [1,2,3].powerset # # => [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]] # # @example Power-set on a Set of strings. # Set['abc', 'xyz', '123'].powerset # # => [#<Set: {}>, #<Set: {"123"}>, #<Set: {"xyz"}>, # #<Set: {"abc"}>, #<Set: {"xyz", "123"}>, # #<Set: {"abc", "123"}>, #<Set: {"abc", "xyz"}>, # #<Set: {"abc", "xyz", "123"}>] # # @see http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/ # def powerset inject([self.class.new]) do |power_set,element| sub_set = [] power_set.each do |i| sub_set << i sub_set << i + [element] end sub_set end end alias cartesian_product powerset end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
combinatorics-0.1.0 | lib/combinatorics/power_set/mixin.rb |