Permutations: Difference between revisions

m
→‎{{header|REXX}}: changed comments, added comments, added whitespace -- ~~~~
m (→‎{{header|REXX}}: changed comments, added comments, added whitespace -- ~~~~)
Line 2,056:
 
call permSets things,bunch,inbetweenChars,names
exit /*stick a fork in it, we're done.*/
exit
/*──────────────────────────────────PERMSETS subroutine─────────────────*/
/*────────────────────────────────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
@abcS=@abcU||@abc; @0abcS=123456789||@abcS
 
do k=1 for x /*build a list of (perm) symbols. */
_=p(word(uSyms,k) p(substr(@0abcS,k,1) k)) /*get or generate a symbol. */
if length(_)\==1 then sep='_' /*if not 1char1st char, then use sep. */
$.k=_ /*append it to the symbol list. */
end
 
if between=='' then between=sep /*use the appropriate separatorseperator. */
list='$. @. between x y'
call permset(1)
return
exit
/*──────────────────────────────────PERMSET subroutine──────────────────*/
/*────────────────────────────────PERMSET subroutine────────────────────*/
permset: procedure expose (list); parse arg ?
if ?>y then do; _=@.1; do j=2 to y; _=_||between||@.j; end; say _; end
else do q=1 for x /*constructionbuild permutation recursively. */
do k=1 for ?-1; if @.k==$.q then iterate q; end /*k*/
@.?=$.q; call permset(?+1)
end /*q*/
return
/*──────────────────────────────────P subroutine (Pick one)─────────────*/
/*────────────────────────────────P subroutine (Pick one)───────────────*/
p: return word(arg(1),1)</lang>
'''output''' when the following was used for input: <tt> 3 3 </tt>
<pre>
Line 2,152:
arg n .; if n=='' then n=3 /*Not specified? Assume default*/
/*populate the first permuatation*/
do pop=1 for n; @.pop=pop ; end; call tell n
do while nextperm(n,0); call tell n; end
exit /*stick a fork in it, we're done.*/
exit
/*──────────────────────────────────────────────────────────────────────*/
tell: procedure expose @.; _=; do j=1 for arg(1);_=_ @.j;end; say _;return
/*──────────────────────────────────────────────────────────────────────*/
nextperm: procedure expose @.; parse arg n,i; nm=n-1
 
do k=nm by -1 for nm; kp=k+1
Line 2,167:
 
if i==0 then return 0
do j=i+1 while @.j<@.i; end
 
do j=i+1 while @.j<@.i; end
 
parse value @.j @.i with @.i @.j
return 1</lang>
'''output'''
<pre style="height:20ex;overflow:scroll">
1 2 3
1 3 2