diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-14 22:49:50 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-14 22:49:50 +0000 |
| commit | 332405ce7eb49aa98b39840a7f270caec6f7a6bd (patch) | |
| tree | a2c8c4a4da4277b6d91531d6287ea0fda6a989d5 /challenge-251/witawayar/cpp | |
| parent | c9abc354625399b4721fb92f1f53ab276fccf7b0 (diff) | |
| parent | a88faaedcd9ad7157f44c31f63a59d2505ac5079 (diff) | |
| download | perlweeklychallenge-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')
| -rw-r--r-- | challenge-251/witawayar/cpp/ch-1.cpp | 40 | ||||
| -rw-r--r-- | challenge-251/witawayar/cpp/ch-2.cpp | 63 |
2 files changed, 103 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"; +} diff --git a/challenge-251/witawayar/cpp/ch-2.cpp b/challenge-251/witawayar/cpp/ch-2.cpp new file mode 100644 index 0000000000..39da0ead44 --- /dev/null +++ b/challenge-251/witawayar/cpp/ch-2.cpp @@ -0,0 +1,63 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include <algorithm> // transform +#include <iostream> // cout +#include <limits> // numeric_limits +#include <sstream> // stringstream +#include <vector> + +int fun(std::vector<std::vector<int>> const& matrix) { + std::size_t n_rows = matrix.size(), n_cols = matrix[0].size(); + std::vector<int> row_minimals, col_maximals; + row_minimals.reserve(n_rows); + col_maximals.reserve(n_cols); + + // Row minimals + std::transform(matrix.cbegin(), matrix.cend(), + std::back_inserter(row_minimals), + [] (std::vector<int> const& row) { + return *std::min_element(row.cbegin(), row.cend()); }); + + // Column maximals + for (std::size_t i{0}; i != n_cols; ++i) { + int col_maximal{std::numeric_limits<int>::min()}; + for (auto row: matrix) { + if (row[i] > col_maximal) { + col_maximal = row[i]; + } + } + col_maximals.push_back(col_maximal); + } + + // Set intersection... + std::sort(row_minimals.begin(), row_minimals.end()); + std::sort(col_maximals.begin(), col_maximals.end()); + std::vector<int> intersections; + std::set_intersection(row_minimals.cbegin(), row_minimals.cend(), + col_maximals.cbegin(), col_maximals.cend(), + std::back_inserter(intersections)); + + return intersections.empty() + ? -1 + : intersections[0]; +} + +int main(void) { + // Tests + std::vector<std::pair<std::vector<std::vector<int>>, int>> tests = { + {{{3, 7, 8}, {9, 11, 13}, {15, 16, 17}}, 15}, + {{{1, 10, 4, 2}, {9, 3, 8, 7}, {15, 16, 17, 12}}, 12}, + {{{7, 8}, {1, 2}}, 7}, + {{{7, 8}, {13, 2}}, -1} + }; + + 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"; +} |
