Sha256: 1738d5bb0d26f40990f961bb6dd46c42c03ccba09cddfdc58beac12ce934bc35
Contents?: true
Size: 1.09 KB
Versions: 3
Compression:
Stored size: 1.09 KB
Contents
class Array # Produces an array of arrays of all possible combinations # of the given arrays in the position given. (Explain me better?) # # require 'facet/arrat/combinations' # # a = %w|a b| # b = %w|a x| # c = %w|x y| # Array.combinations(a, b, c).each { |x| p x } # # produces # # ["a", "a", "x"] # ["a", "a", "y"] # ["a", "x", "x"] # ["a", "x", "y"] # ["b", "a", "x"] # ["b", "a", "y"] # ["b", "x", "x"] # ["b", "x", "y"] # def self.combinations(head, *rest) crest = rest.empty? ? [[]] : combinations(*rest) head.inject([]) { |combs, item| combs + crest.map { |comb| [item] + comb } } end end #__TEST__ if $0 == __FILE__ require 'test/unit' class TestCase < Test::Unit::TestCase r = [ ["a", "a", "x"], ["a", "a", "y"], ["a", "x", "x"], ["a", "x", "y"], ["b", "a", "x"], ["b", "a", "y"], ["b", "x", "x"], ["b", "x", "y"] ] a = %w|a b| b = %w|a x| c = %w|x y| z = Array.combinations(a, b, c) assert_equal( r, z ) end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
facets-0.7.0 | lib/facet/array/combinations.rb |
facets-0.7.1 | lib/facet/array/combinations.rb |
facets-0.7.2 | lib/facet/array/combinations.rb |