Κυριακή 18 Μαΐου 2025

Cartesian Product

https://rosettacode.miraheze.org/wiki/Cartesian_product_of_two_or_more_lists#M2000_Interpreter

This program perform same cartesian products:

(1,2)x(3,4)=((1,3),(1,4),(2,3),(2,4))

(3,4)x(1,2)=((3,1),(3,2),(4,1),(4,2))

(1,2)x()=()

()x(1,2)=()

(1776,1789)x(7,12)x(4,14,23)x(0,1)=((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,2,3)x(30)x(500,100)=((1,30,500),(1,30,100),(2,30,500),(2,30,100),(3,30,500),(3,30,100))

(1,2,3)x()x(500,100)=()

(1,2,3)x(500,100)x()=()

 


module checkit {
showTuple = lambda (a as array) -> {
k=lambda m=1 ->{
shift 2
if m=1 then
m=0:drop
push "("+array#str$(",")+")"
else
push letter$+",("+array#str$(",")+")"
end if
}
="("+a#fold$(k)+")"
}
function cartesian_prod(a as array, b as array) {
rest=[]
aa=each(a)
while aa
bb=each(b)
while bb
data (array(aa), array(bb))
end while
end while
ccc=(,)
if len(rest)>0 then
cc=each(rest)
while cc
ccc=stackitem(cc)
d=array([])
dd=each(d)
while dd
ee=each(ccc)
while ee
data array(dd)#end((array(ee),))
end while
end while
end while
end if
=array([])
}
open "out.txt" for output as #f
print #f,"(1,2)x(3,4)=";showTuple(cartesian_prod((1,2), (3,4)))
print #f,"(3,4)x(1,2)=";showTuple(cartesian_prod((3,4), (1,2)))
print #f,"(1,2)x()=";showTuple(cartesian_prod((1,2), (,)))
print #f,"()x(1,2)=";showTuple(cartesian_prod((,), (1,2)))
print #f,"(1776,1789)x(7,12)x(4,14,23)x(0,1)=";showTuple(cartesian_prod((1776,1789), (7,12), (4,14,23), (0,1)))
print #f,"(1,2,3)x(30)x(500,100)=";showTuple(cartesian_prod((1,2,3), (30,), (500,100)))
print #f,"(1,2,3)x()x(500,100)=";showTuple(cartesian_prod((1,2,3), (,), (500,100)))
print #f,"(1,2,3)x(500,100)x()=";showTuple(cartesian_prod((1,2,3), (,), (500,100), (,)))
close #f
win notepad, dir$+"out.txt"
}
checkit

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.