Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Cartesian Product in Ruby?

http://spark-university.s3.amazonaws.com/berkeley-saas/homework/hw1.pdf

Trying to part 7 of this assignment. The following code does not seem to work, but to be frank I'm not sure why, the auto-grader leaves the comments that follow the code.

class CartesianProduct
    include Enumerable

    def initialize(arr1 = [], arr2 = [])
        @arr1 = arr1
        @arr2 = arr2
    end

    def each
        prod = []
        @arr1.each do |i|
            @arr2.each do |j|
                prod << [i, j]
            end
        end
        prod.each
    end
end

On Time 
CartesianProduct

Failures:

  1) CartesianProduct should work for the first example given in the homework [15 points]
     Failure/Error: c.to_a.should include([:a, 4],[:a,5],[:b,5],[:b,4])
       expected [] to include [:a, 4], [:a, 5], [:b, 5], and [:b, 4]
       Diff:
       @@ -1,2 +1,2 @@
       -[[:a, 4], [:a, 5], [:b, 5], [:b, 4]]
       +[]

  2) CartesianProduct should work for other examples for 2x2 [20 points]
     Failure/Error: c.to_a.should include([11, 13],[11, 14],[12, 13],[12, 14])
       expected [] to include [11, 13], [11, 14], [12, 13], and [12, 14]
       Diff:
       @@ -1,2 +1,2 @@
       -[[11, 13], [11, 14], [12, 13], [12, 14]]
       +[]

  3) CartesianProduct should work for 3x3 and 4x4 [40 points]
     Failure/Error: c.to_a.should include([1, 4],[1, 5],[1, 6],[2, 4],[2, 5],[2, 6],[3, 4],[3, 5],[3, 6])
       expected [] to include [1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], and [3, 6]
       Diff:
       @@ -1,2 +1,2 @@
       -[[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]]
       +[]

Finished in 0.00631 seconds
5 examples, 3 failures

Failed examples:

rspec /tmp/rspec20130406-910-bhzxkp.rb:32 # CartesianProduct should work for the first example given in the homework [15 points]
rspec /tmp/rspec20130406-910-bhzxkp.rb:47 # CartesianProduct should work for other examples for 2x2 [20 points]
rspec /tmp/rspec20130406-910-bhzxkp.rb:55 # CartesianProduct should work for 3x3 and 4x4 [40 points]

Can someone explain to me what the errors are, so I can fix them in my code? I'm new to Ruby, so I am actually not to sure what the auto-grader is saying...

Some example inputs and outputs: [:a, :b, :c], [4, 5] #=> [ [:a,4], [:a,5], [:b,4], [:b,5], [:c,4], [:c,5] ] For this case, my code works.

like image 264
Rishi Avatar asked Jan 17 '26 07:01

Rishi


2 Answers

Use Array#product

p [:a, :b, :c].product [4, 5]

output:

[[:a, 4], [:a, 5], [:b, 4], [:b, 5], [:c, 4], [:c, 5]]
like image 103
Arup Rakshit Avatar answered Jan 20 '26 00:01

Arup Rakshit


Your each implementation is faulty. You're calling each on the array of pairs, but you're not passing through the block that was passed to your each method. One way of fixing this would be to define your method as

def each(&block)
  #setup prod
  prod.each(&block)
end

Which takes the block passed to your each method and ensures that it is also used when calling each on the product array you have computed

like image 31
Frederick Cheung Avatar answered Jan 20 '26 01:01

Frederick Cheung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!