Best shuffle: Difference between revisions

Fixed formatting D code
(→‎{{header|AutoHotkey}}: added output, added Score(), removed error message)
(Fixed formatting D code)
Line 933:
{{works with|D|2}}
Translation of [[Best_shuffle#Icon_and_Unicon|Icon]] via [[Best_shuffle#AWK|AWK]]
<lang d>import std.stdio, std.random, std.algorithm, std.conv, std.range, std.typecons;
std.typecons;
 
auto bestShuffle(in dstring o){
auto s = o.dup;
randomShuffle(s);
}
foreach (i, ref ci; s) {
if (ci != o[i]) continue;
continue;
foreach (j, ref cj; s)
if (ci != cj && ci != o[j] && cj != o[i]) {
Line 946 ⟶ 949:
}
}
return tuple(s, count!q{a[0] == a[1]}(zip(s, o)));
}</lang>
 
return tuple(s, count!q{ a[0] == a[1] }(zip(s, o)));
<lang d>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("abracadabra"d)[1] == 0);
assert(bestShuffle("immediately"d)[1] == 0);
Line 966 ⟶ 962:
assert(bestShuffle("a"d)[1] == 1);
assert(bestShuffle(""d)[1] == 0);
}
 
<lang d>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>
===Alternative version:===
<lang d>import std.stdio, std.algorithm, std.range;
Anonymous user