diff options
| author | mustafaaydn <isaidwhynot@protonmail.com> | 2024-01-08 01:23:47 +0300 |
|---|---|---|
| committer | mustafaaydn <isaidwhynot@protonmail.com> | 2024-01-08 01:23:47 +0300 |
| commit | e35db9dbd7076795db94cf9d150dc14f879d9fa5 (patch) | |
| tree | 35fe0ca2c02bd2e670bc1c770e919e72b082b2b7 /challenge-250/witawayar/cpp | |
| parent | 15b8ccd187aacd81975c1f91b0be4680d99621f0 (diff) | |
| download | perlweeklychallenge-club-e35db9dbd7076795db94cf9d150dc14f879d9fa5.tar.gz perlweeklychallenge-club-e35db9dbd7076795db94cf9d150dc14f879d9fa5.tar.bz2 perlweeklychallenge-club-e35db9dbd7076795db94cf9d150dc14f879d9fa5.zip | |
add solutions
Diffstat (limited to 'challenge-250/witawayar/cpp')
| -rw-r--r-- | challenge-250/witawayar/cpp/ch-1.cpp | 31 | ||||
| -rw-r--r-- | challenge-250/witawayar/cpp/ch-2.cpp | 37 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-250/witawayar/cpp/ch-1.cpp b/challenge-250/witawayar/cpp/ch-1.cpp new file mode 100644 index 0000000000..0f665202a8 --- /dev/null +++ b/challenge-250/witawayar/cpp/ch-1.cpp @@ -0,0 +1,31 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include <iostream> +#include <sstream> +#include <vector> + +int fun(std::vector<int> ints) { + for (size_t i = 0; i < ints.size(); ++i) { + if ((int) i % 10 == ints[i]) + return i; + } + return -1; +} + +int main(void) { + std::vector<std::pair<std::vector<int>, int>> tests = { + {{0, 1, 2}, 0}, + {{4, 3, 2, 1}, 2}, + {{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, -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"; +} diff --git a/challenge-250/witawayar/cpp/ch-2.cpp b/challenge-250/witawayar/cpp/ch-2.cpp new file mode 100644 index 0000000000..290e1efc60 --- /dev/null +++ b/challenge-250/witawayar/cpp/ch-2.cpp @@ -0,0 +1,37 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-2.cpp +#include <iostream> +#include <sstream> +#include <vector> + +int fun(std::vector<std::string> alpha_num_strs) { + int max = -1; + for (const std::string& str : alpha_num_strs) { + int current = + str.find_first_not_of("0123456789") == std::string::npos + ? std::stoi(str) + : str.size(); + if (current > max) { + max = current; + } + } + return max; +} + +int main(void) { + std::vector<std::pair<std::vector<std::string>, int>> tests = { + {{"perl", "2", "000", "python", "r4ku"}, 6}, + {{"001", "1", "000", "0001"}, 1}, + {{"-12", "2"}, 3} + }; + + 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"; +} |
