Best shuffle: Difference between revisions

(change task desciption)
Line 628:
 
=={{header|D}}==
{{works with|D|2}}
<lang d>import std.stdio, std.random, std.algorithm, std.array, std.conv, std.range, std.typecons;
 
intauto bestShuffle(in dchar[] s1) {
 
bool hasSamePositions(in dchar[] r1, in dchar[] r2, uint ignore) {
Line 654 ⟶ 655:
} while (hasSamePositions(s1, s2, numToIgnore));
}
writefln ("%s %s (%s)", s1, s2, numToIgnore);
 
return tuple(s2, numToIgnore);
}
 
void main(string[] args) {
if (args.length > 1) {
string entry = join(args[1..$], " ");
auto res = bestShuffle(to!dstring(entry));
writefln("%s : %s (%s)", entry, res[0], res[1]);
}
}</lang>
 
<lang d>unittest {
assert(bestShuffle("immediately"d.dup)[1] == 0);
assert(bestShuffle("seesaw"d.dup)[1] == 0);
assert(bestShuffle("abracadabra"d.dup)[1] == 0);
assert(bestShuffle("pop"d.dup)[1] == 1);
assert(bestShuffle("aup"d.dup)[1] == 10);
assert(bestShuffle("aa"d.dup)[1] == 02);
assert(bestShuffle("grrrrrra"d.dup)[1] == 51);
assert(bestShuffle(""d.dup)[1] == 0);
assert(bestShuffle("grrrrrr"d.dup)[1] == 5);
}</lang>
 
Anonymous user