Permutations/Rank of a permutation: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, implemented some semantic changes.
m (added whitespace before the TOC (table of contents), added other whitespace to the task's preamble.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, implemented some semantic changes.)
Line 965:
 
=={{header|REXX}}==
The   '''permsets'''   subroutine (actually, a function) is a modified version of mya REXX subroutine used elsewhere in Rosetta code, .
 
thisThis modified version starts the permute numbers with   '''0'''   (zero)   instead of   '''1'''.
 
Since this REXX program generates permutations without recursive calls,
testing for the limit for a ''stack overflow'' wouldn't be possible using this REXX program.
<lang rexx>/*REXX program showsdisplays permutations of N number of objects (1, 2, 3, ... ···). */
parse arg N seed .; if N=='' then N=3 /*Notobtain specified?optional arguments from Assumethe defaultCL*/
permutesif N=permsets(='' | N)=="," then N=4 /*returnsNot specified? N! Then (#use ofthe permutations)default.*/
say; if datatype(seed\==,'W') then call random ,,seed /*seedcan make RANDOM numbers repeatabilityrepeatable. */
w=length(permutes) /*used for aligning the SAY stuff*/
permutes=permSets(N) /*returns N! (number of permutations).*/
 
w=length(permutes) do what=0 to permutes-1 /*traipseused for aligning the throughSAY each permuteoutput. */
z=permsets(N, what) /*get the "what" permuation. */
say N 'items, permute' right(what,w) "=" z ' rank=' permsets(N,,z)
end /*what*/
 
do what=0 to permutes-1 /*traipse through each of the permutes.*/
say; if seed\=='' then call random ,,seed /*seed ≡ repeatability.*/
z=permsetspermSets(N, what) /*get thewhich of "what"the permuation.permutation it is.*/
say N 'items, permute' right(what,w) "=" z ' rank=' permsetspermSets(N,,z)
end /*what*/
say
N=12
do 4; ?=random(0, N**4) /*REXX has a 100k RANDOM range. */
say N 'items, permute' right(?,6) " is " permsetspermSets(N,?)
end /*rand*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────PERMSETS subroutine─────────────────*/
permsetspermSets: procedure expose @. #; #=0; parse arg x,r,c; c=space(c); xm=x-1
 
do j=1 for x; @.j=j-1; end /*j*/; _=0; do u=2 for xm; _=_ @.u; end /*uj*/
if r==# then return _=0; if c do u=2 for xm; _=_ then@.u; returnend #/*u*/
if r==# then return _; if c==_ then return #
 
do while .permsetspermSets(x,0); #=#+1; _=@.1; do u=2 for xm; _=_ @.u; end /*u*/
do v=2 for xm; _=_ @.v; end /*v*/
if r==# then return _; if c==_ then return #
if r==# then return _; if c==_ then return #
end /*while···*/
return #+1
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permsetspermSets: procedure expose @.; parse arg p,q; pm=p-1
do k=pm by -1 for pm; kp=k+1; if @.k<@.kp then do; q=k; leave; end
end /*k*/
 
do j=q+1 while j<p; parse value @.j @.p with @.p @.j; p=p-1
.permsets: procedure expose @.; parse arg p,q; pm=p-1
do k=pm by -1 for pm; kp=k+1; if @.k<@.kp then do; q=k; leave; end; end /*j*/
do j=q+1 while j<p; parse value @.j @.p withif @.pq==0 @.j; p=p-1;then return end0
do p=q+1 while @.p<@.q; end /*p*/
if q==0 then return 0
do j=q+1 while @.j<@.q; end; parse value @.jp @.q with @.q @.jp
return 1</lang>
{{out}}'''output''' &nbsp; when using the default inputinputs:
<pre>
<pre style="overflow:auto;height:250px">
4 items, permute 0 = 0 1 2 3 rank= 0
4 items, permute 1 = 0 1 3 2 rank= 1
Line 1,030 ⟶ 1,038:
4 items, permute 23 = 3 2 1 0 rank= 23
 
12 items, permute 804710243 is 0 1 2 3 5 9 6 4 7 8 7 11 5 10 9
12 items, permute 14942 4231 is 0 1 2 3 64 10 11 96 7 8 5 49 108
12 items, permute 12250 422 is 0 1 2 3 6 8 4 5 9 118 10 7 106 11
12 items, permute 2491212 is 0 1 2 3 4 56 810 65 9 107 118 711
</pre>