Jump to content

Permutations/Rank of a permutation: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, used a template for output, changed wording in the REXX section header, changed program to also show a particular permutation rank.
m (→‎{{header|REXX}}: added/changed comments and whitespace, used a template for output, changed wording in the REXX section header, changed program to also show a particular permutation rank.)
Line 1,448:
 
=={{header|REXX}}==
The   '''permsets'''   subroutine (actually, a function) is a modified version of a REXX subroutine used elsewhere in Rosetta codeCode.
 
This 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 possiblenecessary using this REXX program.
<lang rexx>/*REXX program displays permutations of N number of objects (1, 2, 3, ···). */
parse arg N y seed . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=4 4 /*Not specified? Then use the default.*/
if y=='' | y=="," then y=17 /* " " " " " " */
if datatype(seed,'W') then call random ,,seed /*can make RANDOM numbers repeatable. */
permutes= permSets(N) /*returns N! (number of permutations).*/
w= length(permutes) /*used for aligning the SAY output. */
@.=
 
do whatp=0 to permutes-1 /*traipse through each of the permutes.*/
z=permSets(N, whatp) /*get which of the permutation it is.*/
say 'for' N ' "items, permute' rank" right(whatp,w) "=" z 'is: rank=' permSets(N,, z)
@.p=z /*define a rank permutation in @ array.*/
end /*whatp*/
say
say /* [↓] displays a particular perm rank*/
N=12
say ' the permutation rank of' y "is: " @.y /*display a particular permuation rank.*/
do 4; ?=random(0, N**4) /*REXX has a 100k RANDOM range. */
say N 'items, permute' right(?,6) " is " permSets(N,?)
end /*rand*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSets: 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 ju=12 for xxm; _=_ @.j=j-1u; end /*ju*/
_ if r=0=# then return _; do u=2 for xm; _ if c==_ @.u; then endreturn /*u*/#
if r==# then return _; do while .permSets(x,0); #=#+1; if c_==_ then return #@.1
do v=2 for xm; _=_ @.v; end /*v*/
 
do whileif .permSets(x,0); #r==#+1; then return _; if c=@.1=_ then return #
end do v=2 for xm; _=_ @.v; end /*vwhile···*/
if r==# then return _; if c==_ then return #+1
end /*while···*/
return #+1
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permSets: procedure expose @.; parse arg p,q; pm=p-1
Line 1,488 ⟶ 1,485:
end /*k*/
 
do j=q+1 while j<p; parse value @.j @.p with @.p @.j; p=p -1
end /*j*/
if q==0 then return 0
Line 1,494 ⟶ 1,491:
parse value @.p @.q with @.q @.p
return 1</lang>
'''{{out|output''' |text=&nbsp; when using the default inputs:}}
<pre>
for 4 items, permute rank 0 =is: 0 1 2 3 rank= 0
for 4 items, permute rank 1 =is: 0 1 3 2 rank= 1
for 4 items, permute rank 2 =is: 0 2 1 3 rank= 2
for 4 items, permute rank 3 =is: 0 2 3 1 rank= 3
for 4 items, permute rank 4 =is: 0 3 1 2 rank= 4
for 4 items, permute rank 5 =is: 0 3 2 1 rank= 5
for 4 items, permute rank 6 =is: 1 0 2 3 rank= 6
for 4 items, permute rank 7 =is: 1 0 3 2 rank= 7
for 4 items, permute rank 8 =is: 1 2 0 3 rank= 8
for 4 items, permute rank 9 =is: 1 2 3 0 rank= 9
for 4 items, permute rank 10 =is: 1 3 0 2 rank= 10
for 4 items, permute rank 11 =is: 1 3 2 0 rank= 11
for 4 items, permute rank 12 =is: 2 0 1 3 rank= 12
for 4 items, permute rank 13 =is: 2 0 3 1 rank= 13
for 4 items, permute rank 14 =is: 2 1 0 3 rank= 14
for 4 items, permute rank 15 =is: 2 1 3 0 rank= 15
for 4 items, permute rank 16 =is: 2 3 0 1 rank= 16
for 4 items, permute rank 17 =is: 2 3 1 0 rank= 17
for 4 items, permute rank 18 =is: 3 0 1 2 rank= 18
for 4 items, permute rank 19 =is: 3 0 2 1 rank= 19
for 4 items, permute rank 20 =is: 3 1 0 2 rank= 20
for 4 items, permute rank 21 =is: 3 1 2 0 rank= 21
for 4 items, permute rank 22 =is: 3 2 0 1 rank= 22
for 4 items, permute rank 23 =is: 3 2 1 0 rank= 23
 
12 items, permutethe permutation 10243rank of 17 is: 0 1 2 3 61 4 7 8 11 5 10 90
12 items, permute 4231 is 0 1 2 3 4 10 11 6 7 5 9 8
12 items, permute 422 is 0 1 2 3 4 5 9 8 10 7 6 11
12 items, permute 1212 is 0 1 2 3 4 6 10 5 9 7 8 11
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.