aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/witawayar/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-14 22:49:50 +0000
committerGitHub <noreply@github.com>2024-01-14 22:49:50 +0000
commit332405ce7eb49aa98b39840a7f270caec6f7a6bd (patch)
treea2c8c4a4da4277b6d91531d6287ea0fda6a989d5 /challenge-251/witawayar/cpp/ch-1.cpp
parentc9abc354625399b4721fb92f1f53ab276fccf7b0 (diff)
parenta88faaedcd9ad7157f44c31f63a59d2505ac5079 (diff)
downloadperlweeklychallenge-club-332405ce7eb49aa98b39840a7f270caec6f7a6bd.tar.gz
perlweeklychallenge-club-332405ce7eb49aa98b39840a7f270caec6f7a6bd.tar.bz2
perlweeklychallenge-club-332405ce7eb49aa98b39840a7f270caec6f7a6bd.zip
Merge pull request #9397 from mustafaaydn/challenge_251
Solutions for the 251st challenge
Diffstat (limited to 'challenge-251/witawayar/cpp/ch-1.cpp')
-rw-r--r--challenge-251/witawayar/cpp/ch-1.cpp40
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";
+}