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