Best shuffle: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added whitespace, allowed input from command line, split up multiple lines, added/changed comments, undented subroutine fences. -- ~~~~ |
|||
Line 2,246:
=={{header|REXX}}==
<lang rexx>/*REXX program to find the best shuffle (for a character string). */
parse arg list /*get words from the command line*/
if list='' then list='tree abracadabra seesaw elk grrrrrr up a' /*def.?*/
w=0 /*widest word , for prettifing. */
do i=1 for words(list)
end /*i*/
w=w+5 /*add five spaces to widest word.*/
do n=1 for words(list) /*process the words in the list. */
$=word(list,n) /*the original word in the list. */
new=bestShuffle($) /*shufflized version of the word.*/
say 'original:' left($,
end /*n*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BESTSHUFFLE subroutine──────────────*/
bestShuffle: procedure; parse arg x 1 ox; Lx=length(x)
if Lx<3 then return reverse(x) /*fast track these puppies. */
do j=1 for Lx-1
a=substr(x,j ,1)
b=substr(x,j+1,1); if a\==b then iterate
_=verify(x,a); if _==0 then iterate /*switch 1st rep with some char. */
y=substr(x,_,1); x=overlay(a,x,_)
Line 2,271 ⟶ 2,274:
end /*j*/
do k=1 for Lx
a=substr(x, k,1)
b=substr(ox,k,1);
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*/
return x
/*──────────────────────────────────KSAME procedure─────────────────────*/
kSame: procedure; parse arg x,y; k=0
do m=1 for min(length(x),length(y))
k
end /*m*/
return k</lang>
|