Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/rubyplb.rb,
lib/rubyplb/ary_with_combination.rb
Constant Summary collapse
- @@combi_indices_hash =
{}
Instance Method Summary collapse
- #combi_indices(s, n) ⇒ Object
- #combination(n) ⇒ Object
- #subset ⇒ Object
- #subsets ⇒ Object
- #true_subsets ⇒ Object
Instance Method Details
#combi_indices(s, n) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/rubyplb/ary_with_combination.rb', line 4 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 |
#combination(n) ⇒ Object
17 18 19 20 |
# File 'lib/rubyplb/ary_with_combination.rb', line 17 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 |
#subset ⇒ Object
26 27 28 29 30 |
# File 'lib/rubyplb.rb', line 26 def subset (0..self.length).inject([]) do |ret, n| ret.push(*self.combination(n)) end end |
#subsets ⇒ Object
22 23 24 25 26 |
# File 'lib/rubyplb/ary_with_combination.rb', line 22 def subsets (0..length).inject([]) do |ret, n| ret.push(*combination(n)) end.uniq end |
#true_subsets ⇒ Object
28 29 30 31 32 |
# File 'lib/rubyplb/ary_with_combination.rb', line 28 def true_subsets (1..length).inject([]) do |ret, n| ret.push(*combination(n)) end.uniq end |