Best shuffle: Difference between revisions

→‎{{header|D}}: more concise
(→‎{{header|D}}: more concise)
Line 631:
<lang d>import std.stdio, std.random, std.algorithm, std.conv, std.range, std.typecons;
 
auto bestShuffle(in dstring s1o){
auto s2s = s1o.dup;
randomShuffle(s2s);
foreach(i;, 0ref ..ci; s2.lengths)
foreach(j;, 0ref ..cj; s2.lengths)
if (i != j && s2[i]ci != s1o[j] && s2[j]cj != s1o[i]) {
swap(s2[i]ci, s2[j]cj);
break;
}
 
return tuple(s2s, count!q{a[0] == a[1]}(zip(s1s,s2 o)));
}</lang>
 
Anonymous user