Best shuffle: Difference between revisions
no edit summary
m (→{{header|Wren}}: Changed to Wren S/H) |
No edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 2,204:
end.
</syntaxhighlight>
=={{header|EasyLang}}==
{{trans|C}} (deterministic)
<syntaxhighlight>
proc best_shuffle s$ . r$ diff .
l = len s$
for c$ in strchars s$
s[] &= strcode c$
.
len cnt[] 128
for i to l
cnt[s[i]] += 1
max = higher max cnt[s[i]]
.
for i to 128
while cnt[i] > 0
cnt[i] -= 1
buf[] &= i
.
.
r[] = s[]
for i to l
for j to l
if r[i] = buf[j]
r[i] = buf[(j + max) mod1 l] mod 128
if buf[j] <= 128
buf[j] += 128
.
break 1
.
.
.
diff = 0
r$ = ""
for i to l
diff += if r[i] = s[i]
r$ &= strchar r[i]
.
.
for s$ in [ "abracadabra" "seesaw" "elk" "grrrrrr" "up" "a" ]
best_shuffle s$ r$ d
print s$ & " " & r$ & " " & d
.
</syntaxhighlight>
{{out}}
<pre>
abracadabra brabacadaar 0
seesaw wssaee 0
elk kel 0
grrrrrr rgrrrrr 5
up pu 0
a a 1
</pre>
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 2,212 ⟶ 2,265:
extension op
{
}
public program()
{
}</syntaxhighlight>
{{out}}
Line 2,263 ⟶ 2,316:
The best shuffle of a is a(1)
</pre>
=={{header|Erlang}}==
Deterministic version.
Line 2,315 ⟶ 2,369:
"a" "a" 1
</pre>
=={{header|FreeBASIC}}==
{{trans|Liberty BASIC}}
Line 3,388 ⟶ 3,385:
up, pu, (0)
a, a, (1)</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program BestShuffle;
Const
arr : array[1..6] Of string = ('abracadabra','seesaw','elk','grrrrrr','up','a');
Function Shuffle(inp: String): STRING;
Var x,ReplacementDigit : longint;
ch : char;
Begin
If length(inp) > 1 Then
Begin
Randomize;
For x := 1 To length(inp) Do
Begin
Repeat
ReplacementDigit := random(length(inp))+1;
Until (ReplacementDigit <> x);
ch := inp[x];
inp[x] := inp[ReplacementDigit];
inp[ReplacementDigit] := ch;
End;
End;
shuffle := inp;
End;
Function score(OrgString,ShuString : String) : integer;
Var i : integer;
Begin
score := 0;
For i := 1 To length(OrgString) Do
If OrgString[i] = ShuString[i] Then inc(score);
End;
Var i : integer;
shuffled : string;
Begin
For i := low(arr) To high(arr) Do
Begin
shuffled := shuffle(arr[i]);
writeln(arr[i],' , ',shuffled,' , (',score(arr[i],shuffled),')');
End;
End.
</syntaxhighlight>
{{out}}
<pre>
abracadabra , baraadacbar , (3)
seesaw , esaews , (0)
elk , ekl , (1)
grrrrrr , rrgrrrr , (5)
up , up , (2)
a , a , (1)
</pre>
=={{header|Perl}}==
The Algorithm::Permute module does not ship with perl, but is freely available from CPAN.
|