Permutations: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: restructured the REXX program to use less space on RC. -- ~~~~)
Line 2,036: Line 2,036:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to find the missing permutation. */
<lang rexx>/*REXX program generates all permutations of N different objects. */


/*inbetweenChars & names are optional.*/


/*inbetweenChars & names are optional.*/
parse arg things bunch inbetweenChars names
parse arg things bunch inbetweenChars names


Line 2,048: Line 2,046:
call permSets things,bunch,inbetweenChars,names
call permSets things,bunch,inbetweenChars,names
exit
exit
/*────────────────────────────────PERMSETS subroutine───────────────────*/

permSets: procedure; parse arg x,y,between,uSyms /*X things Y at a time.*/

@.=; sep= /*X can't be>length(@0abcs).*/
/*──────────────────────────────────────────────────────────────────────*/
@abc='abcdefghijklmnopqrstuvwxyz'; @abcU=@abc; upper @abcU
permSets: procedure; parse arg x,y,between,usyms /*X things Y at a time.*/
@abcS=@abcU||@abc; @0abcS=123456789||@abcS
/*X can't be > length(@0abcs). */
@abc='abcdefghijklmnopqrstuvwxyz'
@abcu=@abc; upper @abcu
@abcs=@abcu||@abc
@0abcs=123456789||@abcs
@.=''
sep=''


do k=1 for x /*build list of symbols. */
do k=1 for x /*build list of symbols. */
_=p(word(usyms,k) p(substr(@0abcs,k,1) k)) /*get or generate a symbol. */
_=p(word(uSyms,k) p(substr(@0abcS,k,1) k)) /*get or generate a symbol. */
if length(_)\==1 then sep='_' /*if not 1char, then use sep*/
if length(_)\==1 then sep='_' /*if not 1char, then use sep*/
$.k=_ /*append to the sumbol list.*/
$.k=_ /*append to the symbol list.*/
end
end


if between=='' then between=sep /*use appropriate seperator.*/
if between=='' then between=sep /*use appropriate seperator.*/

list='$. @. between x y'
list='$. @. between x y'
call permset(1)
call permset(1)
exit
exit

/*────────────────────────────────PERMSET subroutine────────────────────*/
/*────────────────────────────────PERMSET subroutine────────────────────*/
permset: procedure expose (list); parse arg ?
permset: procedure expose (list); parse arg ?
if ?>y then do; _=@.1; do j=2 to y; _=_||between||@.j; end; say _; end

if ?>y then do
_=@.1
do j=2 to y
_=_||between||@.j
end
say _
end
else do q=1 for x /*construction permutation recursively*/
else do q=1 for x /*construction permutation recursively*/
do k=1 for ?-1
do k=1 for ?-1; if @.k==$.q then iterate q; end /*k*/
if @.k==$.q then iterate q
@.?=$.q; call permset(?+1)
end
end /*q*/
@.?=$.q
call permset(?+1)
end
return
return

/*────────────────────────────────P subroutine (Pick one)───────────────*/
/*────────────────────────────────P subroutine (Pick one)───────────────*/
p: return word(arg(1),1)</lang>
p: return word(arg(1),1)</lang>
{{out|Output for input <tt>3 3</tt>}}
'''output''' when the following was used for input: <tt> 3 3 </tt>
<pre>
<pre>
123
123
Line 2,102: Line 2,081:
321
321
</pre>
</pre>
{{out|Output for input <tt>4 4 --- A B C D</tt>}}
'''output''' when the following was used for input: <tt> 4 4 --- A B C D </tt>
<pre style="height:15ex;overflow:scroll">
<pre style="height:15ex;overflow:scroll">
A---B---C---D
A---B---C---D
Line 2,129: Line 2,108:
D---C---B---A
D---C---B---A
</pre>
</pre>
Output when the following was used for input:
'''output''' when the following was used for input: <tt> 4 3 - aardvark gnu stegosaurus platypus </tt>
<br><br>
4 3 - aardvark gnu stegosaurus platypus
<pre style="height:15ex;overflow:scroll">
<pre style="height:15ex;overflow:scroll">
aardvark-gnu-stegosaurus
aardvark-gnu-stegosaurus