Jump to content

Cartesian product of two or more lists: Difference between revisions

Added AutoHotkey
(→‎{{header|Lua}}: added another Lua solution)
(Added AutoHotkey)
Line 572:
[]</pre>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>example := [
(join,
[[1, 2], [3, 4]]
[[3, 4], [1, 2]]
[[1, 2], []]
[[], [1, 2]]
[[1776, 1789], [7, 12], [4, 14, 23], [0, 1]]
[[1, 2, 3], [30] , [500, 100]]
[[1, 2, 3], [] , [500, 100]]
)]
 
for i, obj in example
{
Product := CartesianProduct(obj)
out := dispRes(Product)
result .= out "`n`n"
}
MsgBox % result
return
 
dispRes(Product){
for i, o in Product
{
for j, v in o
output .= v ", "
output := Trim(output, ", ")
output .= "], ["
}
return "[[" trim(output, ", []") "]]"
}
 
CartesianProduct(obj){
CP(obj, Product:=[], [])
return Product
}
 
CP(obj, Product, stack, v:=""){
oClone := obj.clone()
oClone.RemoveAt(1)
stack.= v ","
for i, o in obj
{
for j, v in o
CP(oClone, Product, stack, v)
return
}
stack := trim(stack, ",")
oTemp := []
for i, v in StrSplit(stack, ",")
oTemp.Push(v)
Product.push(oTemp)
}</lang>
{{out}}
<pre>[[1, 3], [1, 4], [2, 3], [2, 4]]
[[3, 1], [3, 2], [4, 1], [4, 2]]
[]
[]
[[1776, 7, 4, 0], [1776, 7, 4, 1], [1776, 7, 14, 0], [1776, 7, 14, 1], [1776, 7, 23, 0], [1776, 7, 23, 1], [1776, 12, 4, 0], [1776, 12, 4, 1], [1776, 12, 14, 0], [1776, 12, 14, 1], [1776, 12, 23, 0], [1776, 12, 23, 1], [1789, 7, 4, 0], [1789, 7, 4, 1], [1789, 7, 14, 0], [1789, 7, 14, 1], [1789, 7, 23, 0], [1789, 7, 23, 1], [1789, 12, 4, 0], [1789, 12, 4, 1], [1789, 12, 14, 0], [1789, 12, 14, 1], [1789, 12, 23, 0], [1789, 12, 23, 1]]
[[1, 30, 500], [1, 30, 100], [2, 30, 500], [2, 30, 100], [3, 30, 500], [3, 30, 100]]
[]</pre>
=={{header|Bracmat}}==
<lang Bracmat>( ( mul
299

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.