On Wednesday, 25 March 2015 at 20:09:53 UTC, bearophile wrote:
This is still not very efficient (perhaps the last sorting has
to be stable):
void main() {
import std.stdio, std.algorithm, std.typecons, std.array;
[7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8,
5, 8, 8]
.sort()
.groupBy!((a, b) => a == b)
.map!array
.array
.sort!q{a.length > b.length}
.joiner
.writeln;
}
On Wednesday, 25 March 2015 at 20:53:43 UTC, Ivan Kazmenko wrote:
5. An efficient version would be to count the integers by using
an associative array (or a redBlackTree for guaranteed upper
bound) and then use these. It is O (n) time and memory spent
in precalculation phase and O (n log n) time for sorting.
Looks like there is no way to write that as a chain of
transforms, but many optimizations do require manual labor.
-----
import std.algorithm, std.conv, std.range, std.stdio;
void main () {
auto arr = [7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1,
1, 1, 2, 2, 8, 5, 8, 8];
int [int] counts;
foreach (e; arr) {
counts[e]++;
}
arr.multiSort !((a, b) => counts[a] > counts[b], (a, b) =>
a < b);
arr.map !(to !(string))
.join (" ")
.writeln;
// prints 1 1 1 1 1 3 3 3 3 3 5 5 5 5 8 8 8 2 2 7 7 0
}
-----
Thank you very much, Ivan and bearophile!
On Wednesday, 25 March 2015 at 20:53:43 UTC, Ivan Kazmenko wrote:
Also, some of my previously posted codes do not compile under
2.066 or earlier unless you replace .join (' ') with .join ("
") there.
I have long been updated :)