Skip to content

Instantly share code, notes, and snippets.

@KarenWest
Created April 26, 2013 17:00
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/5468736 to your computer and use it in GitHub Desktop.
Save KarenWest/5468736 to your computer and use it in GitHub Desktop.
Output from code is in comments at bottom - not sure what I'm doing wrong here, as you will see in
the output.
# -*- 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 initialize(seq1, seq2)
puts "Enumerable initialize"
puts "seq1"
puts seq1
puts "seq2"
puts seq2
self.seq1 = seq1
self.seq2 = seq2
#cartesion_product(seq1,seq2)
end
#def method_missing(seq1,seq2)
#puts "Enumerable method_missing"
#end
def cartesion_product(seq1,seq2)
puts "cartesion_product()"
puts seq1
puts seq2
seq1.each do |elt1|
puts "elt1"
puts elt1
seq2.each do |elt2|
puts "elt2"
puts elt2
puts "yielding"
puts elt1
puts elt2
yield [elt1,elt2]
end
end
end
end
class CartesianProduct
include Enumerable
# YOUR CODE HERE
def method_missing(seq1=[],seq2=[],seq3=[])
puts "method_missing() for CartesionProduct class--up to 3 args"
puts "seq1"
puts seq1
puts "seq2"
puts seq2
puts "seq3"
puts seq3
listcartProd = []
cartCoord = cartesion_product(seq2,seq3)
puts cartCoord
#listcartProd.concat([x,y])
#return listcartProd
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
Enumerable initialize
seq1
a
b
seq2
4
5
method_missing() for CartesionProduct class--up to 3 args
seq1
seq1=
seq2
a
b
seq3
cartesion_product()
a
b
elt1
a
elt1
b
a
b
method_missing() for CartesionProduct class--up to 3 args
seq1
seq2=
seq2
4
5
seq3
cartesion_product()
4
5
elt1
4
elt1
5
4
5
method_missing() for CartesionProduct class--up to 3 args
seq1
each
seq2
seq3
cartesion_product()
Enumerable initialize
seq1
a
b
seq2
method_missing() for CartesionProduct class--up to 3 args
seq1
seq1=
seq2
a
b
seq3
cartesion_product()
a
b
elt1
a
elt1
b
a
b
method_missing() for CartesionProduct class--up to 3 args
seq1
seq2=
seq2
seq3
cartesion_product()
method_missing() for CartesionProduct class--up to 3 args
seq1
each
seq2
seq3
cartesion_product()
Enumerable initialize
seq1
a
b
c
seq2
4
5
method_missing() for CartesionProduct class--up to 3 args
seq1
seq1=
seq2
a
b
c
seq3
cartesion_product()
a
b
c
elt1
a
elt1
b
elt1
c
a
b
c
method_missing() for CartesionProduct class--up to 3 args
seq1
seq2=
seq2
4
5
seq3
cartesion_product()
4
5
elt1
4
elt1
5
4
5
method_missing() for CartesionProduct class--up to 3 args
seq1
each
seq2
seq3
cartesion_product()
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment