Anonymous user
Best shuffle: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, changed indentations.
m (→{{header|REXX}}: added/changed whitespace and comments, changed indentations.) |
|||
Line 2,526:
=={{header|REXX}}==
<lang rexx>/*REXX program
parse arg
if
w=0 /*
do i=1 for words(
w=max(w, length(word(
end /*i*/ /* [↑] ··· finds the widest word in @.*/
w=w+
do n=1 for words(
$=word(
new=bestShuffle($) /*get a shufflized version of the word.*/
say 'original:' left($,w) 'new:' left(new,w) 'count:' kSame($,new)
end /*n*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────BESTSHUFFLE
bestShuffle: procedure; parse arg x 1 ox; Lx=length(x)
if Lx<3 then return reverse(x) /*fast track these
do j=1 for Lx-1; jp=j+1 /* [↓] handle any possible
a=substr(x,j ,1)
b=substr(x,j+1,1); if a\==b then iterate /*ignore replicates.*/
_=verify(x,a); if _==0 then iterate /*switch 1st
y=substr(x,_,1); x=overlay(a,x,_)
x=overlay(y,x,j)
rx=reverse(x); _=verify(rx,a); if _==0 then iterate /*¬enough
y=substr(rx,_,1); _=lastpos(y,x) /*switch 2nd
x=overlay(a,x,_);
end /*j*/
do k=1 for Lx /*
a=substr( x,
b=substr(ox,k,1); if a\==b then iterate /*skip replicate. */
if k==Lx then x=left(x,k-2)a || substr(x,k-1,1)
else x=left(x,k-1)substr(x,k+1,1)a || substr(x,k+2)
end /*k*/
return x
/*──────────────────────────────────KSAME
kSame: procedure; parse arg x,y; k=0
▲ end /*m*/
return k</lang>
'''output''' (with a freebie thrown in):
<pre>
original: tree new: eert count: 0
original: abracadabra new: baaracadrab count: 0
original: seesaw new: eswase count: 0
original: elk new: lke count: 0
original: grrrrrr new: rrrrrrg count: 5
original: up new: pu count: 0
original: a new: a count: 1
</pre>
|