Question: Please help with this Cloud9 Ruby programming project. Given two collections (of possibly different lengths), we want to get the Cartesian product of the sequencesin

Please help with this Cloud9 Ruby programming project.

Given two collections (of possibly different lengths), we want to get the Cartesian product of the sequencesin other words, every possible pair of N elements where one element is drawn from each collection.

For example, the Cartesian product of the sequences a==[:a,:b,:c] and b==[4,5] is: ab == [[:a,4],[:a,5],[:b,4],[:b,5],[:c,4],[:c,5]]

Create a method that accepts two sequences and returns an iterator that will yield the elements of the Cartesian product, one at a time, as a two-element array.

It doesn't matter what order the elements are returned in. So for the above example, the ordering [[:a,4], [:b,4], [:c,4], [:a,5], [:b,5], [:c,5]] would be correct, as would any other ordering.

It does matter that within each pair, the order of the elements matches the order in which the original sequences were provided. That is, [:a,4] is a member of the Cartesian product ab, but [4,:a] is not. (Although [4,:a] is a member of the Cartesian product ba.]

class CartesianProduct
include Enumerable
# YOUR CODE HERE

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!