Ordered partitions: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: provisional fix) |
m (→{{header|REXX}}: added/changed comments and whitespace, added whitespace to the output, centered the output better.) |
||
Line 1,764: | Line 1,764: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program displays ordered partitions: |
<lang rexx>//*REXX program displays the ordered partitions as: 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 |
call orderedPartitions 1,2,0,1 /*Note: 1,2,,1 will also work. */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
orderedPartitions: procedure; #=arg(); |
orderedPartitions: procedure; #=arg(); bot.=; top.=; low=; high=; d=123456789 |
||
t=0 /*T: is the sum of all the arguments.*/ |
t=0 /*T: is the sum of all the arguments.*/ |
||
do i=1 for #; |
do i=1 for #; t=t + arg(i) /*sum all the highest numbers in parts.*/ |
||
end /*i*/ |
end /*i*/ /* [↑] may have an omitted argument. */ |
||
hdr= ' partitions for: ' /*define the start of the header text. */ |
|||
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 |
len.j=max(1, _) /*LEN: length of args. «0 is 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 number for range.*/ |
||
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 the digits used for VERIFY. */ |
||
hdr=hdr _ /*build (by appending) display header.*/ |
hdr=hdr _ /*build (by appending) 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 /*the low and high numbers for DO below*/ |
||
end /*j*/ |
end /*j*/ |
||
/* [↓] same as: okD=left('0'd, t+1) */ |
|||
/*define the legal digits to be used. */ |
|||
okD=left(0 || d, t + 1) /*define the legal digits to be used. */ |
okD=left(0 || d, t + 1) /*define the legal digits to be used. */ |
||
say |
say; hdr=center(hdr" ", 60, '═'); say hdr /*display centered title for the output*/ |
||
say /*show a blank line (as a separator). */ |
|||
say |
|||
do g=low to high /* [↑] generate the ordered partitions*/ |
do g=low to high /* [↑] generate the ordered partitions*/ |
||
if verify(g, okD) \==0 then iterate /*filter out unwanted |
if verify(g, okD) \==0 then iterate /*filter out unwanted partitions (digs)*/ |
||
p=1 /*P: is the position of a decimal dig.*/ |
p=1 /*P: is the position of a decimal dig.*/ |
||
$= /*$: will be the transformed numbers. */ |
$= /*$: will be the transformed numbers. */ |
||
Line 1,794: | Line 1,795: | ||
if verify(_, @.k) \==0 then iterate g /*is the decimal digit not valid ? */ |
if verify(_, @.k) \==0 then iterate g /*is the decimal digit not valid ? */ |
||
!= /* [↓] validate the decimal number. */ |
!= /* [↓] validate the decimal number. */ |
||
if @.k\==0 then do j=1 for length(_); |
if @.k\==0 then do j=1 for length(_); z=substr(_, j, 1) /*get a dig.*/ |
||
if pos(z, $)\==0 then iterate g |
if pos(z, $)\==0 then iterate g /*previous ?*/ |
||
!=!','z |
!=!','z /*add comma.*/ |
||
if j==1 |
if j==1 then iterate /*is firstt?*/ |
||
if z<=substr(_, j-1, 1) then iterate g |
if z<=substr(_, j-1, 1) then iterate g /*ordered ?*/ |
||
if pos(z, _, 1+pos(z,_))\==0 then iterate g |
if pos(z, _, 1 +pos(z, _))\==0 then iterate g /*duplicate?*/ |
||
end /*j*/ |
end /*j*/ |
||
p=p + len.k /*point to the next decimal |
p=p + len.k /*point to the next decimal digit (num)*/ |
||
$=$ ' {'strip( |
$=$ ' {'strip(translate(!, ,0), ,",")'}' /*dress number up by suppessing LZ ··· */ |
||
end /*k*/ |
end /*k*/ |
||
say |
say center($, length(hdr) ) /*display numbers in ordered partition.*/ |
||
end /*g*/ |
end /*g*/ |
||
say |
|||
return</lang> |
return</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= 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 ═════════════════ |
|||
{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> |
</pre> |
||