Best shuffle: Difference between revisions
Content added Content deleted
(→Swap if it is locally better algorithm: Shortened output) |
(→{{header|D}}: more concise) |
||
Line 631: | Line 631: | ||
<lang d>import std.stdio, std.random, std.algorithm, std.conv, std.range, std.typecons; |
<lang d>import std.stdio, std.random, std.algorithm, std.conv, std.range, std.typecons; |
||
auto bestShuffle(in dstring |
auto bestShuffle(in dstring o){ |
||
auto |
auto s = o.dup; |
||
randomShuffle( |
randomShuffle(s); |
||
foreach(i |
foreach(i, ref ci; s) |
||
foreach(j |
foreach(j, ref cj; s) |
||
if (i != j && |
if (i != j && ci != o[j] && cj != o[i]) { |
||
swap( |
swap(ci, cj); |
||
break; |
break; |
||
} |
} |
||
return tuple( |
return tuple(s, count!q{a[0] == a[1]}(zip(s, o))); |
||
}</lang> |
}</lang> |
||