Anonymous user
Ordered partitions: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations.
(Added Kotlin) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations.) |
||
Line 1,713:
=={{header|REXX}}==
<lang rexx>/*REXX program displays ordered partitions: orderedPartitions(i, j, k, ···). */
call orderedPartitions 2,0,2 /*Note: 2,,2 will also work. */
call orderedPartitions 1,1,1
call orderedPartitions 1,2,0,1 /*Note: 1,2,1 will also work. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
orderedPartitions: procedure; #=arg(); hdr=; bot.=; top.=; low=; high=; d=123456789
/* [↓] process each of the arguments. */
do j=1 for #; _=arg(j) /* _: is the
end /*j*/
okD=left(0 || d, t+1) /*define the legal digits to be used. */
say center(' partitions for: ' hdr" ",
say
do g=low to high /* [↑]
if verify(g, okD)\==0 then iterate /*filter out
p=1 /*P: is the position of a
$= /*$: will be the transformed
do k=1 for # /*verify the partitions numbers. */
/*validate
_=substr(g,p,len.k) /*ordered
if verify(_, @.k)\==0 then iterate g
!=
if @.k\==0 then do j=1 for length(_); z=substr(_, j, 1)
if pos(z, $)\==0 then iterate g /*
!=!','z
if j==1 then iterate /*is firstt?*/
if z<=substr(_, j-1, 1) then iterate g /*
if pos(z, _, 1+pos(z,_))\==0 then iterate g /*
end /*j*/
p=p + len.k
$=$ ' {'strip( translate(!, ,0), ,
end /*k*/
say ' ' $ /*display numbers in ordered partition.*/
end /*g*/
say
return</lang>
<pre>
───────────────── partitions for: 2 0 2 ──────────────────
{1,2} {} {3,4}
{1,3} {} {2,4}
{1,4} {} {2,3}
{2,3} {} {1,4}
{2,4} {} {1,3}
{3,4} {} {1,2}
───────────────── partitions for: 1 1 1 ──────────────────
{1} {2} {3}
{1} {3} {2}
{2} {1} {3}
{2} {3} {1}
{3} {1} {2}
{3} {2} {1}
{1} {2,3} {} {4}
{1} {2,4} {} {3}
{1} {3,4} {} {2}
{2} {1,3} {} {4}
{2} {1,4} {} {3}
{2} {3,4} {} {1}
{3} {1,2} {} {4}
{3} {1,4} {} {2}
{3} {2,4} {} {1}
{4} {1,2} {} {3}
{4} {1,3} {} {2}
{4} {2,3} {} {1}
</pre>
|