aboutsummaryrefslogtreecommitdiff
path: root/challenge-225/deadmarshal/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-225/deadmarshal/cpp')
-rw-r--r--challenge-225/deadmarshal/cpp/ch-1.cpp31
-rw-r--r--challenge-225/deadmarshal/cpp/ch-2.cpp26
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-225/deadmarshal/cpp/ch-1.cpp b/challenge-225/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..17fb182b49
--- /dev/null
+++ b/challenge-225/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,31 @@
+#include<iostream>
+#include<vector>
+#include<cctype>
+
+std::size_t max_words(const std::vector<std::string> &vec)
+{
+ size_t count{},max{};
+ for(const auto & s : vec)
+ {
+ for(const auto &c : s) if(isspace(c)) count++;
+ if(max < count) max = count;
+ count = 0;
+ }
+ return max+1;
+}
+
+int main(int argc, char **argv)
+{
+ std::vector<std::string>
+ vec{"Perl and Raku belong to the same family.",
+ "I love perl.",
+ "The Perl and Raku Conference."};
+ std::vector<std::string>
+ vec2{"The Weekly Challenge.",
+ "Python is the most popular guest language.",
+ "Team PWC has over 300 members."};
+ std::cout << max_words(vec) << '\n';
+ std::cout << max_words(vec2) << '\n';
+ return 0;
+}
+
diff --git a/challenge-225/deadmarshal/cpp/ch-2.cpp b/challenge-225/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..bf93226008
--- /dev/null
+++ b/challenge-225/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,26 @@
+#include<iostream>
+#include<vector>
+
+template<typename T>
+void left_right_sum_diff(const std::vector<T> &vec)
+{
+ std::size_t left{},right{};
+ for(std::size_t i = 1; i < vec.size(); ++i) right += vec.at(i);
+ for(std::size_t i = 0; i < vec.size(); ++i)
+ {
+ std::cout << abs(left - right) << ' ';
+ left += vec.at(i);
+ right -= i < vec.size()-1 ? vec.at(i+1) : 0;
+ }
+ std::cout << '\n';
+}
+
+int main()
+{
+ std::vector<int> vec1{10,4,8,3},vec2{1},vec3{1,2,3,4,5};
+ left_right_sum_diff<int>(vec1);
+ left_right_sum_diff<int>(vec2);
+ left_right_sum_diff<int>(vec3);
+ return 0;
+}
+