Anonymous user
Permutations by swapping: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→{{header|Lua}}) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 1,564:
=={{header|REXX}}==
<lang rexx>/*REXX program generates all permutations of N different objects by swapping. */
parse arg things bunch . /*get optional arguments from the C.L. */
things = p(things 4) /*should use the default for THINGS ? */
bunch = p(bunch things) /* " " " " " BUNCH ? */
call permSets things, bunch /*invoke permutations by swapping sub. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; !=1; do j=2 to arg(1); !=!*j; end
c: return substr(arg(1), arg(2), 1) /*pick a single character from a string*/
p: return word(arg(1), 1) /*pick 1st word (or number) from a list*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSets: procedure; parse arg x,y /*take X things Y at a time. */
!.=0; pad=left('',x*y)
@abc = 'abcdefghijklmnopqrstuvwxyz';
@abcS= @abcU || @abc; @0abcS=123456789 || @abcS
z= /*define Z to be a null value for start*/
do i=1 for x /*build list of (permutation) symbols. */
z=z || c(@0abcS, i)
end /*i*/
#=1 /*the number of permutations (so far).*/
!.z=1; q=z; s=1; times=!(x)% !(x-y) /*calculate (#) TIMES using factorial.*/
w=max(length(z), length('permute')) /*maximum width of Z and also PERMUTE.*/
say center('permutations for ' x ' things taken ' y " at a time",60,'═')
say
say pad 'permutation' center("permute", w, '─')
say pad '───────────' center("───────", w, '─')
say pad center(#, 11)
do $=1 until #==times /*perform permutation until # of times.*/
do k=1 for x-1 /*step thru things for things-1 times.*/
do m=k+1 to x;
end
#=#+1; s=-s;
end /*
end /*
return /*we're all finished with permutating. */</lang>▼
▲return /*we're all finished with permutating. */</lang>
▲{{out}} when using the default inputs:
<pre>
══════permutations for 4 things taken 4 at a time═══════
|