Skip to content

Instantly share code, notes, and snippets.

@KarenWest
Created April 19, 2013 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KarenWest/5422609 to your computer and use it in GitHub Desktop.
Save KarenWest/5422609 to your computer and use it in GitHub Desktop.
Question -- error at bottom here - ruby interpreter error
# -*- coding: utf-8 -*-
=begin
Karen West - Saas1 - homework#1 question #7 - April 7th, 2013
HW 1-7: Iterators, Blocks, Yield
Given two collections (of possibly different lengths), we want to get the Cartesian product of the sequences. A Cartesian product is a sequence that enumerates every possible pair from the two collections, where the pair consists of one element from each collection. For example, the Cartesian product (denoted by ×) of the sequences a = [:a, :b, :c] and b = [4, 5] is:
a × b = [ [:a,4], [:a,5], [:b,4], [:b,5], [:c,4], [:c,5] ]
Create a constructor for the class CartesianProduct that that takes two sequences as arguments, these values will define the behavior of your object. Define each as an
instance method for CartesianProduct. Your method should return an iterator which yields the cartesian product of the two sequences used in the class' constructor. The iterator should yield the values one at a time as 2 element arrays.
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 well.
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 a × b but [4,:a] is not. (Instead, [4,:a] is a member of the Cartesian product b × a.)
Below is the code skeleton:
class CartesianProduct
include Enumerable
# Your code here
end
Example test cases:
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
# [:a, 4]
# [:a, 5]
# [:b, 4]
# [:b, 5]
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
# Nothing printed since Cartesian product of anything with an empty collection is empty
#Enumerable Example I followed in Fox and Patterson book:
# return every n'th element in an enumerable
module Enumerable
def every_nth(count)
index = 0
self.each do |elt|
yield elt if index % count == 0
index += 1
end
end
end
list = (1..10).to_a # make an array from a range
list.every_nth(3) { |s| print "#{s}, " }
# => 1, 4, 7, 10,
list.every_nth(2) { |s| print "#{s}, " }
# => 1, 3, 5, 7, 9,
=end
module Enumerable
def cartesionProduct(seq1,seq2)
self.seq1.each do |elt1|
self.seq2.each do |elt2|
yield [elt1,elt2]
end
end
end
end
class CartesianProduct
include Enumerable
# YOUR CODE HERE
def initialize(seq1, seq2)
self.seq1 = seq1
self.seq2 = seq2
return cartesionProduct(seq1,seq2)
end
end
#Example test cases:
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
# [:a, 4]
# [:a, 5]
# [:b, 4]
# [:b, 5]
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
# Nothing printed since Cartesian product of anything with an empty collection is empty
#For example, the Cartesian product (denoted by ×) of the sequences a = [:a, :b, :c] and b = [4, 5] is:
#a × b = [ [:a,4], [:a,5], [:b,4], [:b,5], [:c,4], [:c,5] ]
c = CartesianProduct.new([:a,:b, :c], [4,5])
c.each { |elt| puts elt.inspect }
=begin
saasbook@saasbook:~/dev/saas1Take2/hw1$ ruby part7WithTests.rb | more
part7WithTests.rb:69:in `initialize': undefined method `seq1=' for #<CartesianProduct:0x9db3824> (NoMethodError)
from part7WithTests.rb:77:in `new'
from part7WithTests.rb:77:in `<main>'
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment