Anonymous user
Best shuffle: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations.
m (added whitespace before the TOC.) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations.) |
||
Line 2,660:
=={{header|REXX}}==
<lang rexx>/*REXX program
parse arg @ /*get some words from the command line.*/
if @='' then @ = 'tree abracadabra seesaw elk grrrrrr up a' /*use
w=0 /*width of the longest word; for output*/
do i=1 for words(@) /* [↓] process all the words in list. */
w=max(w, length(word(@, i))) /*set the maximum word width (so far). */
end /*i*/ /* [↑] ··· finds the widest word in @.*/
w=w+9 /*add 9 blanks
do n=1 for words(@) /*process all the words in the @ list. */
$=word(@,n) /*get the original word in the @ list. */
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: procedure;
if Lx<3 then return reverse(x) /*fast track
do j=1 for Lx-1;
a=substr(x,j ,1)
b=substr(x,j+1,1);
_=verify(x,a) ; if _==0 then iterate /*
y=substr(x,_,1); x=overlay(a,x,_) /*switch (swap) two ··· */
x=overlay(y,x,j) /* ··· (x,y) characters.*/
rx=reverse(x); _=verify(rx,a)
if _==0 then iterate /*not enough uniquenes.*/
x=overlay(a,x,_); x=overlay(y,x,jp) /*fast way to swap chars. */
end /*j*/
do k=1 for Lx /*handle
a= substr( x, k, 1) /*obtain single character.*/
b= substr(ox, k, 1)
if
else x=left(x, k-1)substr(x, k+1, 1)a || substr(x, k+2)
end /*k*/
return x
/*──────────────────────────────────────────────────────────────────────────────────────*/
kSame: procedure;
do m=1 for min(
end /*m*/
return k</lang>
'''output''' (with a freebie thrown in):
<pre>
|