summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 446e456ddfe201dab9d86446f54b012cb89e3b3c (about) (plain)
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 <cmath>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

const char *TICKS[] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
const size_t NUM_TICKS = 8;
const double EPSILON = 1e-8;

template<typename InputIterator, typename OutputIterator>
void sparkify(InputIterator first, InputIterator last, OutputIterator &out) {
    auto minmax = std::minmax_element(first, last);
    auto min = *minmax.first,
         max = *minmax.second;
    auto values_range = max - min;

    if (abs(values_range) < EPSILON) {
        std::fill_n(out, last - first, TICKS[0]);
    } else {
        auto coef = (NUM_TICKS - 1) / values_range;
        std::transform(first, last, out, [&](auto x) {
            return TICKS[(size_t) round((x - min) * coef)];
        });
    }
}

int main()
{
    std::istream_iterator<double> cin_it(std::cin), eos;
    std::vector<double> values(cin_it, eos);

    if (!std::cin.eof() && std::cin.fail()) {
        std::cerr << "Parse error" << std::endl;
        return 1;
    }
    if (values.empty()) {
        return 0;
    }

    std::ostream_iterator<const char *> cout_it(std::cout);
    sparkify(values.begin(), values.end(), cout_it);
    std::cout << std::endl;
    return 0;
}