Jump to content

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 subroutine────────*/
orderedPartitions: procedure; #=arg(); hdr=; bot.=; top.=; low=; high=; d=123456789
dt=1234567890 /*handy-dandyT: is the sum of literalall forthe digitsarguments.*/
t=0 do i=1 for #; t=t + arg(i) /*T:sum isall the sumhighest ofnumbers allin argumentsparts.*/
doend /*i=1*/ for #; t=t+('0'arg(i)) /*sum all[↑] may have thean highestomitted #sargument. in parts*/
/* [↓] process each of the arguments. */
end /*i*/
do j=1 for #; _=arg(j) /* _: is the /* [↓]Jth argument. process each of arguments*/
do len.j=max(1, _) for #; _=arg(j) /* _: is the Jth argument /*LEN: length of args, 0=special. */
lenbot.j=maxleft(1d, _); if _==0 then bot.j=0 /*define the bottom number. /*LEN: length of args, 0=special*/
bottop.j=right(left(d,t),_); if _==0 then bottop.j=0 /*define the bottom" " top " num*/
top@.j=right(left(d, t),_); if _==0 then top @.j=0 /*define the "digits used for VERIFY. " top " */
@.jhdr=left(d,t);hdr _ if _==0 then @.j=0 /*definebuild (by appending) VERIFYdisplay digitsheader.*/
hdrlow=hdrlow _|| bot.j; high=high || top.j /*the low and high numbers for DO /*build display header.below*/
low=low || bot.j; high=high || top.j /*low and high of loop.*/
end /*j*/
 
okD=left(0 || d, t+1) /*define the legal digits to be used. */
say center(' partitions for: ' hdr" ",50 60, '─'); say/*display centered title for the output*/
say
 
do g=low to high /* [↑] generate orderedthe parts.ordered partitions*/
if verify(g, okD)\==0 then iterate /*filter out the unwanted decimal digits. */
p=1 /*P: is the position of a digitdecimal dig.*/
$= /*$: will be the transformed #snumbers. */
do k=1 for # /*verify the partitions numbers. */
/*validate# number: dups/ordered/repeats*/
_=substr(g,p,len.k) /*ordered partpartition num.number to be tested.*/
if verify(_, @.k)\==0 then iterate g /*is the decimal digit ¬not valid ? */
!= /* [↓] validate numthe decimal number. */
if @.k\==0 then do j=1 for length(_); z=substr(_, j, 1)
if pos(z, $)\==0 then iterate g /*prevprevious. */
!=!','z
if j==1 then iterate /*is firstt?*/
if z<=substr(_, j-1, 1) then iterate g /*ordordered. */
if pos(z, _, 1+pos(z,_))\==0 then iterate g /*dupduplicate.*/
end /*j*/
p=p + len.k /*point to the next #decimal number. */
$=$ ' {'strip( translate(!, ,0), ,'",'")"'}" ' /*dress thenumber #up up.by suppressing LZ ···*/
end /*k*/
say ' ' $ /*display numbers in ordered partition.*/
 
say $
end /*g*/
say
return</lang>
'''{{out|output'''|text=&nbsp; when using the default inputs:}}
<pre>
───────────────── partitions for: 2 0 2 ──────────────────
──────────── 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 ──────────────────
──────────── partitions for: 1 1 1 ─────────────
 
{1} {2} {3}
{1} {3} {2}
{2} {1} {3}
{2} {3} {1}
{3} {1} {2}
{3} {2} {1}
 
─────────────────────────── partitions for: 1 2 0 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>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.