aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/ash/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-078/ash/cpp')
-rw-r--r--challenge-078/ash/cpp/ch-1.cpp30
-rw-r--r--challenge-078/ash/cpp/ch-2.cpp32
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-078/ash/cpp/ch-1.cpp b/challenge-078/ash/cpp/ch-1.cpp
new file mode 100644
index 0000000000..875dc4a005
--- /dev/null
+++ b/challenge-078/ash/cpp/ch-1.cpp
@@ -0,0 +1,30 @@
+/*
+ Task 1 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/
+
+ Comments: https://andrewshitov.com/2020/09/14/the-weekly-challenge-078/
+
+ Compile as:
+ $ g++ -std=c++17 ch-1.cpp
+*/
+
+#include <iostream>
+#include <vector>
+
+using namespace std;
+
+int main() {
+ vector<int> a = {9, 10, 7, 5, 6, 1};
+
+ auto max = a.back();
+ vector<int> leaders = {max};
+ for (auto i = a.rbegin(); i != a.rend(); i++) {
+ if (*i > max) {
+ max = *i;
+ leaders.push_back(max);
+ }
+ }
+
+ for (auto i = leaders.rbegin(); i != leaders.rend(); i++)
+ cout << *i << endl;
+}
diff --git a/challenge-078/ash/cpp/ch-2.cpp b/challenge-078/ash/cpp/ch-2.cpp
new file mode 100644
index 0000000000..c3133a8c06
--- /dev/null
+++ b/challenge-078/ash/cpp/ch-2.cpp
@@ -0,0 +1,32 @@
+/*
+ Task 2 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/
+
+ Comments: https://andrewshitov.com/2020/09/14/the-weekly-challenge-078/
+
+ Compile:
+ $ g++ -std=c++17 ch-2.cpp
+
+ Output:
+
+ $ ./a.out
+ 40 50 10 20 30
+ 50 10 20 30 40
+*/
+
+#include <iostream>
+#include <vector>
+
+using namespace std;
+
+int main() {
+ vector<int> a = {10, 20, 30, 40, 50};
+ vector<int> b = {3, 4};
+
+ for (auto shift : b) {
+ for (auto pos = 0; pos != a.size(); pos++) {
+ cout << a[(pos + shift) % a.size()] << ' ';
+ }
+ cout << endl;
+ }
+}