Sha256: 265f5089cc284548ff1e5bf7de1ebe049ff2d6facf066b089dff3f95c5771f88

Contents?: true

Size: 675 Bytes

Versions: 5

Compression:

Stored size: 675 Bytes

Contents

class Array
  @@combi_indices_hash = {}

  def combi_indices(s,n)
    ret = @@combi_indices_hash[s] ||= []
    if ret.empty?
      s.times{|i|
        ret.dup.each{|a|
          ret.push(a+[i])
        }
        ret.push([i])
      }
    end
    ret.select{|a|a.size==n}
  end

  def combination(n)
    # combination of Ruby 1.9.x returns array with a blank array as its top
    combi_indices(self.size,n).collect{|a| self.values_at(*a)}.unshift([])
  end

  def subsets
    (0..length).inject([]) do |ret, n|
      ret.push(*combination(n))
    end.uniq
  end

  def true_subsets
    (1..length).inject([]) do |ret, n|
      ret.push(*combination(n))
    end.uniq
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubyplb-0.3.0 lib/rubyplb/ary_with_combination.rb
rubyplb-0.2.94 lib/rubyplb/ary_with_combination.rb
rubyplb-0.2.93 lib/rubyplb/ary_with_combination.rb
rubyplb-0.2.92 lib/rubyplb/ary_with_combination.rb
rubyplb-0.2.91 lib/rubyplb/ary_with_combination.rb