Best shuffle: Difference between revisions

Added temporary third D version (will replace the second)
(→‎{{header|AWK}}: Add translation from Icon; this is simpler than the translation from Perl 6.)
(Added temporary third D version (will replace the second))
Line 485:
--34435223--511 (0)
-354431223--51- (0)</pre>
A slower but more compact version based on the J one:
 
<lang d>char[] bestShuffle2(char[] s) {
if (!s.length) return [];
auto result = new char[s.length];
auto t = array(iota(cast(int)s.length));
schwartzSort!((i){return s.count(s[i]);},"b < a",SwapStrategy.stable)(t);
int maxf = reduce!max(map!((c){ return s.count(c); })(s));
auto groups = new int[][](maxf, 0);
foreach (i; 0 .. s.length)
groups[i % maxf] ~= t[i];
auto fgroups1 = reduce!q{a ~ b}(groups);
foreach (ref g; groups)
g = g[1 .. $] ~ g[0];
auto fgroups2 = reduce!q{a ~ b}(groups);
foreach (i; 0 .. s.length)
result[fgroups1[i]] = s[fgroups2[i]];
return result;
}</lang>
=={{header|Haskell}}==
{{trans|Perl 6}}
Anonymous user