From c040e950295d9ef9a3a952b8319ba074a51b1513 Mon Sep 17 00:00:00 2001 From: mustafaaydn Date: Mon, 22 Jan 2024 00:12:03 +0300 Subject: add solutions --- challenge-252/witawayar/cpp/ch-1.cpp | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-252/witawayar/cpp/ch-1.cpp (limited to 'challenge-252/witawayar/cpp/ch-1.cpp') diff --git a/challenge-252/witawayar/cpp/ch-1.cpp b/challenge-252/witawayar/cpp/ch-1.cpp new file mode 100644 index 0000000000..2d06260474 --- /dev/null +++ b/challenge-252/witawayar/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include // transform +#include // cout +#include // accumulate +#include // stringstream +#include + +int fun(std::vector const& ints) { + std::vector out; + const std::size_t N = ints.size(); + std::vector indexes(N); + std::iota(indexes.begin(), indexes.end(), 1u); + std::transform(ints.cbegin(), ints.cend(), + indexes.cbegin(), std::back_inserter(out), + [&](const int& value, const std::size_t& index) { + return N % index == 0 ? value * value : 0; }); + + return std::accumulate(out.cbegin(), out.cend(), 0); +} + +int main(void) { + // Tests + std::vector, int>> tests = { + {{1, 2, 3, 4}, 21}, + {{2, 7, 1, 19, 18, 3}, 63}, + }; + + for (const auto& [input, expected_output] : tests) { + int got = fun(input); + if (got != expected_output) { + std::stringstream error_msg; + error_msg << "Expected " << expected_output << ", got " << got; + throw std::runtime_error(error_msg.str()); + } + } + + std::cout << "done-testing, success\n"; +} -- cgit