-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (32 loc) · 1.05 KB
/
main.cpp
File metadata and controls
45 lines (32 loc) · 1.05 KB
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
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
#include <format>
#include <range/v3/all.hpp>
namespace v = ranges::views;
namespace a = ranges::actions;
namespace g = ranges;
using namespace std;
auto frequency_cout(string const& input) -> vector<pair<char, long>>
{
auto lowered = input
| v::transform([](unsigned char c) { return tolower(c); })
| g::to<string> | a::sort; // "aadrr"
cout << lowered << endl;
auto grouped = lowered
| v::chunk_by([](char x, char y) { return x == y; }); // [[a, a], [d], [r, r]]
cout << grouped << endl;
auto count = grouped
| v::transform([](auto &&r) { return g::distance(r); }); // [2, 1, 2]
cout << v::all(count) << endl;
auto uniqs = lowered | v::unique;
return v::zip(uniqs, count) | g::to<vector>; // [(a, 2), (d, 1), (r, 2)]
}
int main(int agc, char* agv[])
{
auto rng = frequency_cout(string("Radar"));
for (auto const& e: rng)
cout << format("({}:{})", (char)e.first, e.second) << endl;
return 0;
}