def moment_of_truth(a1, a2)
(a1.permutation(2).to_a).product(a2.repeated_permutation(2).to_a).
map { |(e1, e2), (f1, f2)| [e1, f1, e2, f2 ] }
end
moment_of_truth(['a', 'b'], ['x', 'y'])
#=> [["a", "x", "b", "x"], ["a", "x", "b", "y"], ["a", "y", "b", "x"],
# ["a", "y", "b", "y"], ["b", "x", "a", "x"], ["b", "x", "a", "y"],
# ["b", "y", "a", "x"], ["b", "y", "a", "y"]]
moment_of_truth(['a', 'b'], ['x', 'y', 'z'])
#=> [["a", "x", "b", "x"], ["a", "x", "b", "y"], ["a", "x", "b", "z"],
# ["a", "y", "b", "x"], ["a", "y", "b", "y"], ["a", "y", "b", "z"],
# ["a", "z", "b", "x"], ["a", "z", "b", "y"], ["a", "z", "b", "z"],
# ["b", "x", "a", "x"], ["b", "x", "a", "y"], ["b", "x", "a", "z"],
# ["b", "y", "a", "x"], ["b", "y", "a", "y"], ["b", "y", "a", "z"],
# ["b", "z", "a", "x"], ["b", "z", "a", "y"], ["b", "z", "a", "z"]]
moment_of_truth(['a', 'b', 'c'], ['x', 'y'])
#=> [["a", "x", "b", "x"], ["a", "x", "b", "y"], ["a", "y", "b", "x"],
# ["a", "y", "b", "y"], ["a", "x", "c", "x"], ["a", "x", "c", "y"],
# ["a", "y", "c", "x"], ["a", "y", "c", "y"], ["b", "x", "a", "x"],
# ["b", "x", "a", "y"], ["b", "y", "a", "x"], ["b", "y", "a", "y"],
# ["b", "x", "c", "x"], ["b", "x", "c", "y"], ["b", "y", "c", "x"],
# ["b", "y", "c", "y"], ["c", "x", "a", "x"], ["c", "x", "a", "y"],
# ["c", "y", "a", "x"], ["c", "y", "a", "y"], ["c", "x", "b", "x"],
# ["c", "x", "b", "y"], ["c", "y", "b", "x"], ["c", "y", "b", "y"]]
moment_of_truth(['a', 'b', 'c'], ['x', 'y', 'z'])
#=> [["a", "x", "b", "x"], ["a", "x", "b", "y"], ["a", "x", "b", "z"],
# ["a", "y", "b", "x"], ["a", "y", "b", "y"], ["a", "y", "b", "z"],
# ["a", "z", "b", "x"], ["a", "z", "b", "y"], ["a", "z", "b", "z"],
# ["a", "x", "c", "x"], ["a", "x", "c", "y"], ["a", "x", "c", "z"],
# ["a", "y", "c", "x"], ["a", "y", "c", "y"], ["a", "y", "c", "z"],
# ["a", "z", "c", "x"], ["a", "z", "c", "y"], ["a", "z", "c", "z"],
# ["b", "x", "a", "x"], ["b", "x", "a", "y"], ["b", "x", "a", "z"],
# ["b", "y", "a", "x"], ["b", "y", "a", "y"], ["b", "y", "a", "z"],
# ["b", "z", "a", "x"], ["b", "z", "a", "y"], ["b", "z", "a", "z"],
# ["b", "x", "c", "x"], ["b", "x", "c", "y"], ["b", "x", "c", "z"],
# ["b", "y", "c", "x"], ["b", "y", "c", "y"], ["b", "y", "c", "z"],
# ["b", "z", "c", "x"], ["b", "z", "c", "y"], ["b", "z", "c", "z"],
# ["c", "x", "a", "x"], ["c", "x", "a", "y"], ["c", "x", "a", "z"],
# ["c", "y", "a", "x"], ["c", "y", "a", "y"], ["c", "y", "a", "z"],
# ["c", "z", "a", "x"], ["c", "z", "a", "y"], ["c", "z", "a", "z"],
# ["c", "x", "b", "x"], ["c", "x", "b", "y"], ["c", "x", "b", "z"],
# ["c", "y", "b", "x"], ["c", "y", "b", "y"], ["c", "y", "b", "z"],
# ["c", "z", "b", "x"], ["c", "z", "b", "y"], ["c", "z", "b", "z"]]
moment_of_truth(['a', 'b', 'c', 'd'], ['x', 'y', 'z', 'a']) # returns 192 arrays
#=> [["a", "x", "b", "x"], ["a", "x", "b", "y"], ["a", "x", "b", "z"],
# ["a", "x", "b", "a"], ["a", "y", "b", "x"], ["a", "y", "b", "y"],
# ["a", "y", "b", "z"], ["a", "y", "b", "a"], ["a", "z", "b", "x"],
# ...
# ["d", "a", "c", "y"], ["d", "a", "c", "z"], ["d", "a", "c", "a"]]
See Array#permutation, Array#repeated_permutation and Array#product.
Note that because of the asymmetry between how a1 and a2 are treated, no guidance has been provided for how the addition of one more arrays (e.g., a1, a2 and a3) should be treated, so I have not addressed that case.
I don't see how recursion can be used here, but perhaps a reader can prove me wrong.
a,x,b,x,a,y,b,y,b,x,a,x, andb,y,a,y, then where dida,x,a,x, …a,x,a,y,a,y,a,x,a,y,a,y,b,x,b,x,b,x,b,y,b,y,b,x, andb,y,b,ygo? Never forget that computers are very, very dumb, and very, very literal, and they have no creativity, and don't take context into account; they also don't ask clarifying questions. If you cannot even write down what you want to achieve in a way that a human can understand it, then you will have no hope in explaining it to a computer!a1witha2and then again the resulting product of those two products:x, y = a1.map {|el| [el].product(a2)}; x.product(y).a1 = [a,b]references two variables or methods whose values are not known:aandb. If you mean those to be literals you need to write something likea1 = ['a','b']ora1 = [:a,:b]. At heart, it's an interesting question.