Ordered partitions: Difference between revisions

Content added Content deleted
(Added Kotlin)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
Line 1,713: Line 1,713:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program displays ordered partitions: orderedPartitions(i,j,k,···)*/
<lang rexx>/*REXX program displays ordered partitions: orderedPartitions(i, j, k, ···). */
call orderedPartitions 2,0,2 /*Note: 2,,2 will also work*/
call orderedPartitions 2,0,2 /*Note: 2,,2 will also work. */
call orderedPartitions 1,1,1
call orderedPartitions 1,1,1
call orderedPartitions 1,2,0,1 /*Note: 1,2,1 will also work*/
call orderedPartitions 1,2,0,1 /*Note: 1,2,1 will also work. */
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────ORDEREDPARTITIONS subroutine────────*/
orderedPartitions: procedure; #=arg(); hdr=; bot.=; top.=; low=; high=
orderedPartitions: procedure; #=arg(); hdr=; bot.=; top.=; low=; high=; d=123456789
d=123456789 /*handy-dandy literal for digits.*/
t=0 /*T: is the sum of all the arguments.*/
t=0 /*T: is the sum of all arguments.*/
do i=1 for #; t=t + arg(i) /*sum all the highest numbers in parts.*/
do i=1 for #; t=t+('0'arg(i)) /*sum all the highest #s in parts*/
end /*i*/ /* [↑] may have an omitted argument. */
/* [↓] process each of the arguments. */
end /*i*/
/* [↓] process each of arguments*/
do j=1 for #; _=arg(j) /* _: is the Jth argument. */
do j=1 for #; _=arg(j) /* _: is the Jth argument.*/
len.j=max(1, _) /*LEN: length of args, 0=special. */
len.j=max(1,_) /*LEN: length of args, 0=special*/
bot.j=left(d, _); if _==0 then bot.j=0 /*define the bottom number. */
bot.j=left(d,_); if _==0 then bot.j=0 /*define the bottom num*/
top.j=right(left(d,t),_); if _==0 then top.j=0 /* " " top " */
top.j=right(left(d,t),_); if _==0 then top.j=0 /* " " top " */
@.j=left(d, t); if _==0 then @.j=0 /*define the digits used for VERIFY. */
@.j=left(d,t); if _==0 then @.j=0 /*define VERIFY digits.*/
hdr=hdr _ /*build (by appending) display header.*/
hdr=hdr _ /*build display header.*/
low=low || bot.j; high=high || top.j /*the low and high numbers for DO below*/
low=low || bot.j; high=high || top.j /*low and high of loop.*/
end /*j*/
end /*j*/


okD=left(0||d,t+1) /*define legal digits to be used.*/
okD=left(0 || d, t+1) /*define the legal digits to be used. */
say center(' partitions for: ' hdr" ",50,'─'); say
say center(' partitions for: ' hdr" ", 60, '─') /*display centered title for the output*/
say

do g=low to high /* [↑] generate ordered parts. */
do g=low to high /* [↑] generate the ordered partitions*/
if verify(g,okD)\==0 then iterate /*filter out the unwanted digits.*/
if verify(g, okD)\==0 then iterate /*filter out unwanted decimal digits. */
p=1 /*P: is the position of a digit.*/
p=1 /*P: is the position of a decimal dig.*/
$= /*$: will be the transformed #s.*/
$= /*$: will be the transformed numbers. */
do k=1 for # /*verify the partitions numbers. */
do k=1 for # /*verify the partitions numbers. */
/*validate#: dups/ordered/repeats*/
/*validate number: dups/ordered/repeats*/
_=substr(g,p,len.k) /*ordered part num. to be tested.*/
_=substr(g,p,len.k) /*ordered partition number to be tested*/
if verify(_,@.k)\==0 then iterate g /*is digit ¬ valid ? */
if verify(_, @.k)\==0 then iterate g /*is the decimal digit not valid ? */
!= /* [↓] validate num.*/
!= /* [↓] validate the decimal number. */
if @.k\==0 then do j=1 for length(_); z=substr(_,j,1)
if @.k\==0 then do j=1 for length(_); z=substr(_, j, 1)
if pos(z,$)\==0 then iterate g /*prev*/
if pos(z, $)\==0 then iterate g /*previous. */
!=!','z
!=!','z
if j==1 then iterate
if j==1 then iterate /*is firstt?*/
if z<=substr(_,j-1,1) then iterate g /*ord.*/
if z<=substr(_, j-1, 1) then iterate g /*ordered. */
if pos(z,_,1+pos(z,_))\==0 then iterate g /*dup.*/
if pos(z, _, 1+pos(z,_))\==0 then iterate g /*duplicate.*/
end /*j*/
end /*j*/
p=p+len.k /*point to next #.*/
p=p + len.k /*point to the next decimal number. */
$=$ ' {'strip(translate(!,,0),,',')"}" /*dress the # up. */
$=$ ' {'strip( translate(!, ,0), ,",")'}' /*dress number up by suppressing LZ ···*/
end /*k*/
end /*k*/
say ' ' $ /*display numbers in ordered partition.*/

say $
end /*g*/
end /*g*/
say
say
return</lang>
return</lang>
'''output''' when using the default inputs:
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
───────────────── partitions for: 2 0 2 ──────────────────
──────────── partitions for: 2 0 2 ─────────────


{1,2} {} {3,4}
{1,2} {} {3,4}
{1,3} {} {2,4}
{1,3} {} {2,4}
{1,4} {} {2,3}
{1,4} {} {2,3}
{2,3} {} {1,4}
{2,3} {} {1,4}
{2,4} {} {1,3}
{2,4} {} {1,3}
{3,4} {} {1,2}
{3,4} {} {1,2}


───────────────── partitions for: 1 1 1 ──────────────────
──────────── partitions for: 1 1 1 ─────────────


{1} {2} {3}
{1} {2} {3}
{1} {3} {2}
{1} {3} {2}
{2} {1} {3}
{2} {1} {3}
{2} {3} {1}
{2} {3} {1}
{3} {1} {2}
{3} {1} {2}
{3} {2} {1}
{3} {2} {1}


─────────── partitions for: 1 2 0 1 ────────────
──────────────── partitions for: 1 2 0 1 ─────────────────


{1} {2,3} {} {4}
{1} {2,3} {} {4}
{1} {2,4} {} {3}
{1} {2,4} {} {3}
{1} {3,4} {} {2}
{1} {3,4} {} {2}
{2} {1,3} {} {4}
{2} {1,3} {} {4}
{2} {1,4} {} {3}
{2} {1,4} {} {3}
{2} {3,4} {} {1}
{2} {3,4} {} {1}
{3} {1,2} {} {4}
{3} {1,2} {} {4}
{3} {1,4} {} {2}
{3} {1,4} {} {2}
{3} {2,4} {} {1}
{3} {2,4} {} {1}
{4} {1,2} {} {3}
{4} {1,2} {} {3}
{4} {1,3} {} {2}
{4} {1,3} {} {2}
{4} {2,3} {} {1}
{4} {2,3} {} {1}
</pre>
</pre>