0

I am trying to implement a cartesian product of an arbitrary number of lists. I have been thinking about this for hours, and I can't figure it out.

For example, if I have:

list letter = [1, 2, 3] list num = [a, b, c]

then we will print cartesian product of:

  • letter x letter
  • letter x num
  • num x num
  • num x letter

However, if i had a fixed number of lists, it would be easy. But I don't know how to do an arbitrary number of lists.

1 Answers1

-1

If you have a container of lists, simply for each list traverse the container and do the cartesian product.

for each i do 
    for each j do cartesian(a[i], a[j])
gilgil28
  • 540
  • 5
  • 12