tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,34 @@
/*REXX program generates all permutations of N different objects. */
parse arg things bunch inbetweenChars names
/* inbetweenChars (optional) defaults to a [null]. */
/* names (optional) defaults to digits (and letters). */
call permSets things,bunch,inbetweenChars,names
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────.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 /*build permutation recursively. */
do k=1 for ?-1; if @.k==$.q then iterate q; end /*k*/
@.?=$.q; call .permset ?+1
end /*q*/
return
/*──────────────────────────────────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|generate a symbol.*/
if length(_)\==1 then sep='_' /*if not 1st char, then use sep. */
$.k=_ /*append it to the symbol list. */
end
if between=='' then between=sep /*use the appropriate separator. */
list='$. @. between x y'
call .permset 1
return
/*──────────────────────────────────P subroutine (Pick one)─────────────*/
p: return word(arg(1),1)

View file

@ -0,0 +1,22 @@
/*REXX program shows permutations of '''N''' number of objects (1,2,3, ...).*/
parse arg n .; if n=='' then n=3 /*Not specified? Assume default*/
/*populate the first permutation.*/
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.*/
/*──────────────────────────────────NEXTPERM subroutine─────────────────*/
nextperm: procedure expose @.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1
if @.k<@.kp then do; i=k; leave; end
end /*k*/
do j=i+1 while j<n; parse value @.j @.n with @.n @.j; n=n-1; end
if i==0 then return 0
do j=i+1 while @.j<@.i; end
parse value @.j @.i with @.i @.j
return 1
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: procedure expose @.; _=; do j=1 for arg(1);_=_ @.j;end; say _;return