Created
January 10, 2019 15:00
-
-
Save herrkessler/e3b4ac1beb19557ec9420c53e84fb9ae to your computer and use it in GitHub Desktop.
cartesian product example in Ruby, Python and Crystal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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