diff options
author | Samuel Fadel <samuel@nihil.ws> | 2018-07-25 16:16:47 -0300 |
---|---|---|
committer | Samuel Fadel <samuel@nihil.ws> | 2018-07-25 16:16:47 -0300 |
commit | 3b12fea57f9cdae45bf8801dd274eb3669530e1c (patch) | |
tree | 3dddeb540666f84b8dd71c23c063d6e780ae80e6 |
Initial commit
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | src/main.cpp | 48 |
2 files changed, 57 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..79e4712 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +CXXFLAGS=-O2 -Wall + +all: spark + +spark: src/main.cpp + g++ $(CXXFLAGS) -o spark src/main.cpp + +clean: + rm -f spark diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..95b9140 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,48 @@ +#include <cmath> +#include <algorithm> +#include <iostream> +#include <iterator> +#include <sstream> +#include <vector> + +const char *TICKS[] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}; +const size_t NUM_TICKS = 8; +const double EPSILON = 1e-8; + +template<typename T> +void sparkify(const std::vector<T> &values, std::ostringstream &out) { + auto minmax = std::minmax_element(values.begin(), values.end()); + auto min = *minmax.first, + max = *minmax.second; + auto values_range = max - min; + + if (abs(values_range) < EPSILON) { + auto it = std::ostream_iterator<const char *>(out); + std::fill_n(it, values.size(), TICKS[0]); + } else { + auto coef = (NUM_TICKS - 1) / values_range; + for (const T &x: values) { + out << TICKS[(size_t) round((x - min) * coef)]; + } + } +} + +int main() +{ + std::vector<double> values; + std::ostringstream out; + for (double value; std::cin >> value;) { + values.push_back(value); + } + if (!std::cin.eof() && std::cin.fail()) { + std::cerr << "Parse error" << std::endl; + return 1; + } + if (values.empty()) { + return 0; + } + + sparkify(values, out); + std::cout << out.str() << std::endl; + return 0; +} |