Jump to content

Best shuffle: Difference between revisions

m (→‎{{header|Go}}: added more output)
Line 1,053:
===Simpler Version===
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.traits, std.typecons;
 
auto bestShuffle(S)(in dstringS oorig) if (isSomeString!S) {
static if (isNarrowString!S)
auto s = o.dup;
s.randomShuffle auto o = to!dstring(orig);
else alias orig o;
 
auto s = o.dup;
randomShuffle(s);
foreach (i, ref ci; s) {
if (ci != o[i])
Line 1,069 ⟶ 1,072:
}
}
return tuple(s, zip(s, o).count!q{ "a[0] == a[1]"(zip(s, }(o)));
}
 
} unittest {
return tuple(s, zip(s, o).count!q{ a[0] == a[1] }());
} unittest {
assert(bestShuffle("abracadabra"d)[1] == 0);
assert(bestShuffle("immediately"d)[1] == 0);
Line 1,082 ⟶ 1,086:
}
 
void main(/*in*/ string[] args) {
if (args.length > 1) {
string entry = join(args[1 .. $].join(, " ");
auto res = entry.dtext().bestShuffle(entry);
writefln("%s : %s (%ds)", entry, res.tupleof[0], res[1]);
}
}</lang>
 
===Faster Version===
<lang d>import std.stdio, std.algorithm, std.range;
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.