diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2024-01-02 02:29:50 -0500 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2024-01-02 02:29:50 -0500 |
| commit | 754a0f9cce2dc07a97609f2b8bc642dfee61930f (patch) | |
| tree | 7bd79339617fc14720b3bc79d52453563b6e06e9 /challenge-250/deadmarshal/cpp | |
| parent | 5f38c976cae9103ec02e413224d047d8b149956d (diff) | |
| download | perlweeklychallenge-club-754a0f9cce2dc07a97609f2b8bc642dfee61930f.tar.gz perlweeklychallenge-club-754a0f9cce2dc07a97609f2b8bc642dfee61930f.tar.bz2 perlweeklychallenge-club-754a0f9cce2dc07a97609f2b8bc642dfee61930f.zip | |
TWC250
Diffstat (limited to 'challenge-250/deadmarshal/cpp')
| -rw-r--r-- | challenge-250/deadmarshal/cpp/ch-1.cpp | 19 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/cpp/ch-2.cpp | 30 |
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-250/deadmarshal/cpp/ch-1.cpp b/challenge-250/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..ff43839325 --- /dev/null +++ b/challenge-250/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,19 @@ +#include<iostream> +#include<vector> + +template<typename T> +int smallest_index(const std::vector<T> &vec) +{ + for(size_t i = 0; i < vec.size(); ++i) if(i % 10 == vec.at(i)) return i; + return -1; +} + +int main() +{ + std::vector<int> vec1{0,1,2},vec2{4,3,2,1},vec3{1,2,3,4,5,6,7,8,9,0}; + std::cout << smallest_index<int>(vec1) << '\n' + << smallest_index<int>(vec2) << '\n' + << smallest_index<int>(vec3) << '\n'; + return 0; +} + diff --git a/challenge-250/deadmarshal/cpp/ch-2.cpp b/challenge-250/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..0b87ec93b6 --- /dev/null +++ b/challenge-250/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,30 @@ +#include<iostream> +#include<vector> + +bool is_numeric(const std::string &str) +{ + for(const auto& c : str) if(!isdigit(c)) return false; + return true; +} + +template<typename T> +size_t alphanumeric_string_value(const std::vector<T> &vec) +{ + size_t max{}; + for(auto & e : vec) + { + size_t n = is_numeric(e) ? std::stoi(e) : e.size(); + if(n > max) max = n; + } + return max; +} + +int main() +{ + std::vector<std::string> vec1{"perl","2","000","python","r4ku"}, + vec2{"001","1","000","0001"}; + std::cout << alphanumeric_string_value<std::string>(vec1) << '\n' + << alphanumeric_string_value<std::string>(vec2) << '\n'; + return 0; +} + |
