Here is my stab at the problem (I made it as a standalone method, as I
don't think of this operation as something that an array can do:
require 'test/unit'
def cartprod(base, *others)
return base.map{|a| [a] } if others.empty?
others = cartprod(*others)
base.inject([]) { | r, a | others.inject(r) { | r, b | r << ([a, *b]) } }
end
class CarprodTest < Test::Unit::TestCase
def test_simple
assert_equal([[1, 'a'], [1, 'b'],
[2, 'a'], [2, 'b'],
[3, 'a'], [3, 'b']],
cartprod([1,2,3], %w(a b)),
'Simple Cartesian Product failed')
assert_equal([[1, "a"], [1, "b"], [1, "c"],
[2, "a"], [2, "b"], [2, "c"],
[3, "a"], [3, "b"], [3, "c"]],
cartprod(1..3, 'a'..'c'),
'Simple Cartesian Product with ranges failed')
end
def test_multiple
assert_equal([[1, 'a', :A], [1, 'a', :B], [1, 'b', :A], [1, 'b', :B],
[2, 'a', :A], [2, 'a', :B], [2, 'b', :A], [2, 'b', :B]],
cartprod([1,2], %w(a b), [:A, :B]),
'Multiple Cartesian Product failed')
end
def test_array
assert_equal([[1, ["a", "a"]], [1, ["b", "b"]],
[2, ["a", "a"]], [2, ["b", "b"]],
[3, ["a", "a"]], [3, ["b", "b"]]],
cartprod(1..3, [%w(a a), %w(b b)]),
'Cartesian Product with arrays failed')
end
def test_base_cases
assert_equal([], cartprod([]), "Base case empty array is not correct")
assert_equal([[1], [2], [3]], cartprod(1..3), "Base case single
array is not correct")
end
end
regards,
Brian
--
http://www.**--****.com/
Stringed instrument chords: http://www.**--****.com/