From 3b12fea57f9cdae45bf8801dd274eb3669530e1c Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Wed, 25 Jul 2018 16:16:47 -0300 Subject: Initial commit --- Makefile | 9 +++++++++ src/main.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Makefile create mode 100644 src/main.cpp 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 +#include +#include +#include +#include +#include + +const char *TICKS[] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}; +const size_t NUM_TICKS = 8; +const double EPSILON = 1e-8; + +template +void sparkify(const std::vector &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(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 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; +} -- cgit v1.2.3