Best shuffle: Difference between revisions

Content added Content deleted
m (added whitespace before the TOC.)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
Line 2,660: Line 2,660:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds the best shuffle (for any list of words (characters). */
<lang rexx>/*REXX program determins and displays the best shuffle (for any list of words (letters)*/
parse arg @ /*get some words from the command line.*/
parse arg @ /*get some words from the command line.*/
if @='' then @ = 'tree abracadabra seesaw elk grrrrrr up a' /*use default? */
if @='' then @ = 'tree abracadabra seesaw elk grrrrrr up a' /*use the defaults? */
w=0 /*width of the longest word; for output*/
w=0 /*width of the longest word; for output*/
do i=1 for words(@) /* [↓] process all the words in list. */
do i=1 for words(@) /* [↓] process all the words in list. */
w=max(w, length(word(@, i))) /*set the maximum word width (so far). */
w=max(w, length(word(@, i))) /*set the maximum word width (so far). */
end /*i*/ /* [↑] ··· finds the widest word in @.*/
end /*i*/ /* [↑] ··· finds the widest word in @.*/
w=w+9 /*add 9 blanks, the output looks nicer.*/
w=w+9 /*add 9 blanks for output indentation. */
do n=1 for words(@) /*process all the words in the @ list. */
do n=1 for words(@) /*process all the words in the @ list. */
$=word(@,n) /*get the original word in the @ list. */
$=word(@,n) /*get the original word in the @ list. */
new=bestShuffle($) /*get a shufflized version of the word.*/
new=bestShuffle($) /*get a shufflized version of the word.*/
say 'original:' left($,w) 'new:' left(new,w) 'count:' kSame($,new)
say 'original:' left($,w) 'new:' left(new,w) 'count:' kSame($,new)
end /*n*/
end /*n*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────BESTSHUFFLE subroutine────────────────────*/
bestShuffle: procedure; parse arg x 1 ox; Lx=length(x)
bestShuffle: procedure; parse arg x 1 ox; Lx=length(x)
if Lx<3 then return reverse(x) /*fast track these small # of puppies. */
if Lx<3 then return reverse(x) /*fast track small strings*/


do j=1 for Lx-1; jp=j+1 /* [↓] handle any possible replicates.*/
do j=1 for Lx-1; jp=j+1 /* [↓] handle replicates.*/
a=substr(x,j ,1)
a=substr(x,j ,1)
b=substr(x,j+1,1); if a\==b then iterate /*ignore replicates.*/
b=substr(x,j+1,1); if a\==b then iterate /*ignore any replicates. */
_=verify(x,a); if _==0 then iterate /*switch 1st replicate with some char. */
_=verify(x,a) ; if _==0 then iterate /* " " " */
y=substr(x,_,1); x=overlay(a,x,_)
y=substr(x,_,1); x=overlay(a,x,_) /*switch (swap) two ··· */
x=overlay(y,x,j)
x=overlay(y,x,j) /* ··· (x,y) characters.*/
rx=reverse(x); _=verify(rx,a); if _==0 then iterate /*¬enough uniqueness*/
rx=reverse(x); _=verify(rx,a) /*check back end for reps.*/
if _==0 then iterate /*not enough uniquenes.*/
y=substr(rx,_,1); _=lastpos(y,x) /*switch 2nd replicate with later char.*/
x=overlay(a,x,_); x=overlay(y,x,jp) /*OVERLAYs: a fast way to swap chars. */
y=substr(rx,_,1); _=lastpos(y,x) /*switch 2nd rep with char*/
x=overlay(a,x,_); x=overlay(y,x,jp) /*fast way to swap chars. */
end /*j*/
end /*j*/


do k=1 for Lx /*handle cases of possible replicates. */
do k=1 for Lx /*handle a possible rep. */
a=substr( x,k,1)
a= substr( x, k, 1) /*obtain single character.*/
b=substr(ox,k,1); if a\==b then iterate /*skip replicate. */
b= substr(ox, k, 1) /*obtain the original char*/
if k==Lx then x=left(x,k-2)a || substr(x,k-1,1) /*handle last case*/
if a\==b then iterate /*skip non-replications. */
else x=left(x,k-1)substr(x,k+1,1)a || substr(x,k+2)
if k==Lx then x=left(x, k-2)a || substr(x, k-1, 1) /*last case.*/
else x=left(x, k-1)substr(x, k+1, 1)a || substr(x, k+2)
end /*k*/
end /*k*/
return x
return x
/*──────────────────────────────────KSAME procedure───────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
kSame: procedure; parse arg x,y; k=0
kSame: procedure; parse arg x,y; k=0; Lx=length(x); Ly=length(y)
do m=1 for min(length(x), length(y)); k=k + (substr(x,m,1) == substr(y,m,1))
do m=1 for min(Lx, Ly); k=k+(substr(x, m, 1) == substr(y, m, 1))
end /*m*/
end /*m*/
return k</lang>
return k</lang>
'''output''' &nbsp; (with a freebie thrown in):
'''output''' &nbsp; (with a freebie thrown in):
<pre>
<pre>