### Similar Threads:

1.cartesian product of arrays

Hello,
did I miss some standard library function? Anyway, then I'll do it on
my own:
def cartprod(*args)
result = [[]]
while [] != args
t, result = result, []
b, *args = args
t.each do |a|
b.each do |n|
result << a + [n]
end
end
end
result
end
Example:
cartprod([1,2],[3,4,5],[6,7,8])
=> [[1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 4, 6], [1, 4, 7], [1, 4, 8],
[1, 5, 6], [1, 5, 7], [1, 5, 8], [2, 3, 6], [2, 3, 7], [2, 3, 8],
[2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 5, 6], [2, 5, 7], [2, 5, 8]]
Regards
Thomas

2.cartesian product - next to last version

Hello,
Since the original thread got a little long, I decided to post my next to
last version in a new thread. My previous version gave results like
[[1,4],7] for more than two arrays when you really wanted [1,4,7]. The trick
is to flatten each element of the product. The following works for any
number of arrays. Of course you might want a product in which the elements
of that product are arrays. Any suggestions?
class Array
def cartprod(b=[])
if b.empty? then
#assume self an array of arrays
inject {|cp,x| cp.cartprod(x) }
else
z = inject([]) {|a,x| b.inject(a) {|a,y| a << [x,y]}}
z.collect! {|x| x.flatten }
end
end
end
a=[1,2,3]
b=[4,5,6]
c=[7,8,9]
# works fine
p [a,b,c].cartprod
# doesn't work since [1,4,7,[10,11]] is [1,4,7,10,11]
d=[10, [11,12]]
p [a,b,c,d].cartprod

3.cartesian product

Hello,
How would I create a function that will take list (array) of integers and
return their cartesian product?
Thank You,
Walter Kehowski
nuby

4.Ruby Tk and X/Y coordinates

5.Ruby/Tk (X,Y) Coordinate Question

6. ruby graph: coordinate plane?

7. Mark GPS coordinates on image file

8. mouse coordinates in a gnome2 canvas