-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_3_rangeAlgorithms.d
52 lines (45 loc) · 1.24 KB
/
_3_rangeAlgorithms.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
unittest
{
import std.algorithm : canFind, count, equal, map,
filter, sort, uniq, joiner, chunkBy, splitter;
import std.array : array, empty;
import std.range : zip;
import std.stdio : writeln;
import std.string : format;
string text = q{This tour will give you an
overview of this powerful and expressive systems
programming language which compiles directly
to efficient, *native* machine code.};
string res = q{2 -> an, of, to
3 -> and, you
4 -> This, code, give, this, tour, will
5 -> which
7 -> machine, systems
8 -> *native*, compiles, directly, language, overview, powerful
9 -> efficient
10 -> expressive
11 -> programming};
alias pred = c => canFind(" ,.\n", c);
auto words = text.splitter!pred
.filter!(a => !a.empty);
auto wordCharCounts = words
.map!(a => a.count);
/*
* Output the character count
* per word in a nice way
* beginning with least chars!
*/
assert(zip(wordCharCounts, words)
.array()
.sort()
.uniq()
.chunkBy!(a => a[0])
.map!(chunk => format("%d -> %s",
chunk[0],
chunk[1]
.map!(a => a[1])
.joiner(", ")))
.joiner("\n")
.equal(res));
writeln("Test #3 passed");
}