Playing with C++17 via solving "Cracking the Coding Interview" questions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cxx17/cc197.cc

25 lines
556 B

// Return the score for a mastermind guess.
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <range/v3/all.hpp>
using namespace ranges;
template<typename T>
static T max_seq(std::initializer_list<T> seq) {
T top = *seq.begin();
for (auto start : view::ints(0ul, seq.size())) {
for (auto stop : view::ints(start + 1, seq.size())) {
auto sum = accumulate(seq | view::slice(start, stop), 0);
top = std::max(sum, top);
}
}
return top;
}
TEST_CASE("cc19.9", "max_seq") {
CHECK(max_seq({2, -8, 3, -2, 4, -10}) == 5);
}