1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-24 09:41:55 +01:00
CodeWars/4kyu/range_extraction/solution.cpp
Matej Focko 70af121c76
4kyu: add „Range Extraction“
• Also add missing „Sum of Intervals“ to README

Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-08-07 17:19:54 +02:00

68 lines
1.4 KiB
C++

#include <cassert>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
namespace {
struct range_t {
int from;
int to;
range_t(int from, int to) : from(from), to(to) {}
void extend() { to++; }
int size() const { return to - from + 1; }
bool prefixes(int x) const { return x == to + 1; }
};
std::ostream &operator<<(std::ostream &stream, const range_t &range) {
if (range.size() >= 3) {
stream << range.from << "-" << range.to;
} else {
stream << range.from;
for (int i = range.from + 1; i <= range.to; i++) {
stream << "," << i;
}
}
return stream;
}
} // namespace
std::string range_extraction(std::vector<int> args) {
if (args.empty()) {
return "";
}
std::vector<range_t> ranges;
for (int x : args) {
if (ranges.size() && ranges.back().prefixes(x)) {
ranges.back().extend();
} else {
ranges.emplace_back(x, x);
}
}
std::stringstream out;
auto it = ranges.cbegin();
out << (*it);
for (++it; it != ranges.cend(); ++it) {
out << "," << (*it);
}
return out.str();
}
int main() {
assert(range_extraction({-6, -3, -2, -1, 0, 1, 3, 4, 5, 7,
8, 9, 10, 11, 14, 15, 17, 18, 19, 20}) ==
"-6,-3-1,3-5,7-11,14,15,17-20");
assert(range_extraction({-3, -2, -1, 2, 10, 15, 16, 18, 19, 20}) ==
"-3--1,2,10,15,16,18-20");
}