Best shuffle: Difference between revisions
Content added Content deleted
Line 2,257: | Line 2,257: | ||
up, pu, (0) |
up, pu, (0) |
||
a, a, (1)</pre> |
a, a, (1)</pre> |
||
== {{header|Run BASIC}} == |
|||
<lang runbasic>list$ = "abracadabra seesaw pop grrrrrr up a" |
|||
while word$(list$,ii + 1," ") <> "" |
|||
ii = ii + 1 |
|||
w$ = word$(list$,ii," ") |
|||
bs$ = bestShuffle$(w$) |
|||
count = 0 |
|||
for i = 1 to len(w$) |
|||
if mid$(w$,i,1) = mid$(bs$,i,1) then count = count + 1 |
|||
next i |
|||
print w$;" ";bs$;" ";count |
|||
wend |
|||
function bestShuffle$(s1$) |
|||
s2$ = s1$ |
|||
for i = 1 to len(s2$) |
|||
if mid$(s2$,i,1) = mid$(s1$,i,1) then |
|||
for j = 1 to len(s2$) |
|||
if (i <> j) and (mid$(s2$,i,1) <> mid$(s1$,j,1)) and (mid$(s2$,j,1) <> mid$(s1$,i,1)) then |
|||
if j < i then i1 = j:j1 = i else i1 = i:j1 = j |
|||
s2$ = left$(s2$,i1-1) + mid$(s2$,j1,1) + mid$(s2$,i1+1,(j1-i1)-1) + mid$(s2$,i1,1) + mid$(s2$,j1+1) |
|||
end if |
|||
next j |
|||
end if |
|||
next i |
|||
bestShuffle$ = s2$ |
|||
end function</lang> |
|||
Output: |
|||
<pre>abracadabra raabadacabr 0 |
|||
seesaw eswaes 0 |
|||
pop opp 1 |
|||
grrrrrr rgrrrrr 5 |
|||
up pu 0 |
|||
a a 1</pre> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |