Best shuffle: Difference between revisions

(Updates D entries, tags)
(→‎{{header|Pascal}}: add example)
Line 1,580:
'up', 'pu' -> 0
'a', 'a' -> 1</pre>
 
=={{header|Pascal}}==
{{works with|Free_Pascal}}
<lang pascal>program BestShuffleDemo(output);
function BestShuffle(s: string): string;
var
tmp: char;
i, j: integer;
t: string;
begin
t := s;
for i := 1 to length(t) do
for j := 1 to length(t) do
if (i <> j) and (s[i] <> t[j]) and (s[j] <> t[i]) then
begin
tmp := t[i];
t[i] := t[j];
t[j] := tmp;
end;
BestShuffle := t;
end;
const
original: array[1..6] of string =
('abracadabra', 'seesaw', 'elk', 'grrrrrr', 'up', 'a');
 
var
shuffle: string;
i, j, score: integer;
 
begin
for i := low(original) to high(original) do
begin
shuffle := BestShuffle(original[i]);
score := 0;
for j := 1 to length(shuffle) do
if original[i][j] = shuffle[j] then
inc(score);
writeln(original[i], ', ', shuffle, ', (', score, ')');
end;
end.</lang>
Output:
<pre>% ./BestShuffle
abracadabra, caadrbabaar, (0)
seesaw, ewaess, (0)
elk, kel, (0)
grrrrrr, rgrrrrr, (5)
up, pu, (0)
a, a, (1)</pre>
 
=={{header|Perl}}==
Anonymous user