diff options
| author | mustafaaydn <isaidwhynot@protonmail.com> | 2024-01-15 01:07:11 +0300 |
|---|---|---|
| committer | mustafaaydn <isaidwhynot@protonmail.com> | 2024-01-15 01:07:11 +0300 |
| commit | a88faaedcd9ad7157f44c31f63a59d2505ac5079 (patch) | |
| tree | 241f263d9c48099b6f37cbdf5c105a65d25d0432 /challenge-251/witawayar/cpp/ch-1.cpp | |
| parent | dba8c691b150fd0086ac6f95e674d0302f437cac (diff) | |
| download | perlweeklychallenge-club-a88faaedcd9ad7157f44c31f63a59d2505ac5079.tar.gz perlweeklychallenge-club-a88faaedcd9ad7157f44c31f63a59d2505ac5079.tar.bz2 perlweeklychallenge-club-a88faaedcd9ad7157f44c31f63a59d2505ac5079.zip | |
add solutions
Diffstat (limited to 'challenge-251/witawayar/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-251/witawayar/cpp/ch-1.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-251/witawayar/cpp/ch-1.cpp b/challenge-251/witawayar/cpp/ch-1.cpp new file mode 100644 index 0000000000..acc8331116 --- /dev/null +++ b/challenge-251/witawayar/cpp/ch-1.cpp @@ -0,0 +1,40 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include <algorithm> // transform +#include <iostream> // cout +#include <numeric> // accumulate +#include <sstream> // stringstream +#include <vector> + +int fun(std::vector<int> const& ints) { + std::vector<int> out; + std::transform(ints.cbegin(), ints.cend(), + ints.crbegin(), std::back_inserter(out), + [](const int& a, const int& b) { + return std::stoi(std::to_string(a) + std::to_string(b)); }); + std::size_t out_size = out.size(); + int sum = std::accumulate(out.cbegin(), out.cbegin() + out_size / 2, 0); + if (out_size % 2 != 0) + sum += ints[ints.size() / 2]; + + return sum; +} + +int main(void) { + // Tests + std::vector<std::pair<std::vector<int>, int>> tests = { + {{6, 12, 25, 1}, 1286}, + {{10, 7, 31, 5, 2, 2}, 489}, + {{1, 2, 10}, 112} + }; + + 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"; +} |
