Skip to content

Instantly share code, notes, and snippets.

@herrkessler
Created January 10, 2019 15:00
Show Gist options
  • Save herrkessler/e3b4ac1beb19557ec9420c53e84fb9ae to your computer and use it in GitHub Desktop.
Save herrkessler/e3b4ac1beb19557ec9420c53e84fb9ae to your computer and use it in GitHub Desktop.
cartesian product example in Ruby, Python and Crystal
a = (1..2).to_a
b = (1..7).to_a
c = (1..144).to_a
d = (1..37).to_a
e = (1..28).to_a
f = (1..12).to_a
g = (1..16).to_a
result = Array(Array(Int32)).new
Array.each_product([a, b, c, d, e, f, g]) do |x|
result << x
end
p result.size
from itertools import product
a = range(1,2)
b = range(1,7)
c = range(1,144)
d = range(1,37)
e = range(1,28)
f = range(1,12)
g = range(1,16)
all_lists = [a, b, c, d, e, f, g]
print len(list(product(*all_lists)))
a = *(1..2)
b = *(1..7)
c = *(1..144)
d = *(1..37)
e = *(1..28)
f = *(1..12)
g = *(1..16)
multi = [a, b, c, d, e, f, g]
head, *rest = multi
result = head.product(*rest)
p result.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment