aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-250/witawayar/README1
-rw-r--r--challenge-250/witawayar/cpp/ch-1.cpp31
-rw-r--r--challenge-250/witawayar/cpp/ch-2.cpp37
-rw-r--r--challenge-250/witawayar/raku/ch-1.raku38
-rw-r--r--challenge-250/witawayar/raku/ch-2.raku30
5 files changed, 137 insertions, 0 deletions
diff --git a/challenge-250/witawayar/README b/challenge-250/witawayar/README
new file mode 100644
index 0000000000..a52f13edf5
--- /dev/null
+++ b/challenge-250/witawayar/README
@@ -0,0 +1 @@
+Solutions by Mustafa Aydın
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";
+}
diff --git a/challenge-250/witawayar/raku/ch-1.raku b/challenge-250/witawayar/raku/ch-1.raku
new file mode 100644
index 0000000000..1ce031057a
--- /dev/null
+++ b/challenge-250/witawayar/raku/ch-1.raku
@@ -0,0 +1,38 @@
+my &fun = { @^ints.pairs.first({ .key % 10 == .value }, :k) // -1 };
+
+#`{
+- Make use of the implicit signature feature with the @^ twigil
+ - A signature `-> @ints` is automatically generated under the hood
+ - So we have a function accepting a single positional Positional argument :)
+
+- We'd like to reach the indexes as well as the values of the array passed
+ - There are many ways to this end -- `.kv`, `.pairs` are the primary candidates
+ - Since we are looking a "first", and it takes values off of the iterable 1-by-1
+ unlike `map` can do more, we either need `.kv.batch(2)` or `.pairs`
+ - I went for `.pairs` as it is a bit clearer I think
+
+- We now have a stream (a Sequence) of pairs coming into `.first`
+ - .key's of the pairs are 0, 1, 2... and .value's are the elements of @^ints
+
+- Then we check if index modulo 10 is equal to the element itself
+ - `.first` will find the first such "thing"
+ - How that "thing" is presented can be adjusted with "adverb"s
+ - We want the *index* of the first occurence (if any), so we use the `:k` adverb
+ - "k" for key; the default is `:v` for the value itself (`:kv` and `:p` are also available)
+
+- We are almost done -- what if no such index exists to satisfy `.first`?
+ - Then `.first` returns `Nil`, and we need to detect it and return -1 instead
+ - Since `.first(:k, ...)` either returns 0, 1, 2... or Nil, we can check for "defined"ness
+ - In case of an undefined value out of `.first(:k)`, we return -1 via the "defined or" `//` operator
+}
+
+use Test;
+my @tests of Pair =
+ (0, 1, 2) => 0,
+ (4, 3, 2, 1) => 2,
+ (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) => -1;
+
+for @tests -> (:key($input), :value($expected-output)) {
+ ok fun($input) eqv $expected-output;
+ LAST done-testing;
+}
diff --git a/challenge-250/witawayar/raku/ch-2.raku b/challenge-250/witawayar/raku/ch-2.raku
new file mode 100644
index 0000000000..6327fb95b7
--- /dev/null
+++ b/challenge-250/witawayar/raku/ch-2.raku
@@ -0,0 +1,30 @@
+my &fun = { @^alpha-num-strs.map({ /^ <digit>+ $/ ?? +$_ !! .chars }).max };
+
+#`{
+- Make use of the implicit signature feature with the @^ twigil
+ - A signature `-> @alpha-num-strs` is automatically generated under the hood
+ - So we have a function accepting a single positional Positional argument :)
+
+- We need to traverse this array and check if it's full of digits, or not
+ - Traversal is with our good friend `.map` as usual
+ - Checking digits is easy via the premade character class `<digit>`
+ - Full-match is ensured with the regex via the `^` and `$` anchors
+ - Noting that to disallow negative-number strings, e.g., `"-12"`, `+$_` route is dismissed
+
+- The match was successfull?? Then map to numberified version. It wasn't!! Then take the string length.
+ - It's a bit sad that ?? (blunder in chess) corresponds to success and !! (extraordinary move)
+ corresponds to failure but what can you do...
+
+- The `.max` method concludes the challenge
+}
+
+use Test;
+my @tests of Pair =
+ ("perl", "2", "000", "python", "r4ku") => 6,
+ ("001", "1", "000", "0001") => 1,
+ ("-12", "2") => 3;
+
+for @tests -> (:key($input), :value($expected-output)) {
+ ok fun($input) eqv $expected-output;
+ LAST done-testing;
+}