aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-29 02:16:30 +0000
committerGitHub <noreply@github.com>2024-01-29 02:16:30 +0000
commitf193e95f770bb46d18c1a69d507e33cfc574622f (patch)
tree0b7a1a5d7a059db43335bc7c6af0571e7e32a693
parentb3d01f8327d7fdcf7b713be5fda922a4afbf8d1a (diff)
parent3fa6b95dd9cb2cfc0d45652a06bf91e36678b67f (diff)
downloadperlweeklychallenge-club-f193e95f770bb46d18c1a69d507e33cfc574622f.tar.gz
perlweeklychallenge-club-f193e95f770bb46d18c1a69d507e33cfc574622f.tar.bz2
perlweeklychallenge-club-f193e95f770bb46d18c1a69d507e33cfc574622f.zip
Merge pull request #9476 from mustafaaydn/challenge_253
Solutions for the 253rd challenge
-rw-r--r--challenge-253/witawayar/cpp/ch-1.cpp46
-rw-r--r--challenge-253/witawayar/cpp/ch-2.cpp58
-rw-r--r--challenge-253/witawayar/raku/ch-1.raku18
-rw-r--r--challenge-253/witawayar/raku/ch-2.raku28
4 files changed, 150 insertions, 0 deletions
diff --git a/challenge-253/witawayar/cpp/ch-1.cpp b/challenge-253/witawayar/cpp/ch-1.cpp
new file mode 100644
index 0000000000..9f0c138e5e
--- /dev/null
+++ b/challenge-253/witawayar/cpp/ch-1.cpp
@@ -0,0 +1,46 @@
+// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp
+#include <algorithm> // remove_if
+#include <iostream> // cout
+#include <sstream> // stringstream
+#include <vector>
+
+std::vector<std::string> fun(std::vector<std::string> const& words, std::string separator) {
+ std::vector<std::string> parts;
+ std::size_t sep_size = separator.size();
+
+ for (auto& sub_word : words) {
+ std::size_t start = 0;
+ for (size_t found = sub_word.find(separator);
+ found != std::string::npos;
+ found = sub_word.find(separator, start)) {
+ parts.emplace_back(sub_word.begin() + start, sub_word.begin() + found);
+ start = found + sep_size;
+ }
+ if (start != sub_word.size())
+ parts.emplace_back(sub_word.begin() + start, sub_word.end());
+ }
+ parts.erase(std::remove_if(parts.begin(), parts.end(), [](std::string const& str) {
+ return str.find_first_not_of(' ') == std::string::npos; }), parts.end());
+
+ return parts;
+}
+
+int main(void) {
+ // Tests
+ std::vector<std::pair<std::pair<std::vector<std::string>, std::string>, std::vector<std::string>>> tests = {
+ {{{"one.two.three", "four.five", "six"}, "."}, {"one", "two", "three", "four", "five", "six"}},
+ {{{"$perl$$", "$$raku$"}, "$"}, {"perl", "raku"}}
+ };
+
+ for (const auto& [input, expected_output] : tests) {
+ auto [words, separator] = input;
+ auto got = fun(words, separator);
+ if (got != expected_output) {
+ std::stringstream error_msg;
+ error_msg << "Failed at " << separator;
+ throw std::runtime_error(error_msg.str());
+ }
+ }
+
+ std::cout << "done-testing, success\n";
+}
diff --git a/challenge-253/witawayar/cpp/ch-2.cpp b/challenge-253/witawayar/cpp/ch-2.cpp
new file mode 100644
index 0000000000..cd14a85981
--- /dev/null
+++ b/challenge-253/witawayar/cpp/ch-2.cpp
@@ -0,0 +1,58 @@
+// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-2.cpp
+#include <algorithm> // transform
+#include <iostream> // cout
+#include <numeric> // accumulate
+#include <sstream> // stringstream
+#include <tuple> // tie
+#include <vector>
+
+std::vector<int> fun(std::vector<std::vector<int>> const& matrix) {
+ std::vector<std::pair<int, std::vector<int>>> aug_matrix;
+ for (std::size_t idx{0}; idx < matrix.size(); ++idx) {
+ aug_matrix.emplace_back(idx, matrix[idx]);
+ }
+ std::sort(aug_matrix.begin(), aug_matrix.end(),
+ [](auto& vec_a, auto& vec_b) {
+ auto a_sum = std::accumulate(vec_a.second.cbegin(), vec_a.second.cend(), 0);
+ auto b_sum = std::accumulate(vec_b.second.cbegin(), vec_b.second.cend(), 0);
+ return std::tie(a_sum, vec_a.first) < std::tie(b_sum, vec_b.first);
+ });
+ std::vector<int> sorter_indexes;
+ std::transform(aug_matrix.cbegin(), aug_matrix.cend(),
+ std::back_inserter(sorter_indexes),
+ [](auto& pair) { return pair.first; });
+ return sorter_indexes;
+}
+
+int main(void) {
+ // Tests
+ std::vector<std::pair<std::vector<std::vector<int>>, std::vector<int>>> tests = {
+ {
+ {
+ {1, 1, 0, 0, 0},
+ {1, 1, 1, 1, 0},
+ {1, 0, 0, 0, 0},
+ {1, 1, 0, 0, 0},
+ {1, 1, 1, 1, 1}
+ }, {2, 0, 3, 1, 4}},
+ {
+ {
+ {1, 0, 0, 0},
+ {1, 1, 1, 1},
+ {1, 0, 0, 0},
+ {1, 0, 0, 0}
+ }, {0, 2, 3, 1}}
+ };
+
+ std::size_t test_case{0};
+ for (const auto& [input, expected_output] : tests) {
+ auto got = fun(input);
+ if (got != expected_output) {
+ std::stringstream error_msg;
+ error_msg << "Failed at " << test_case++ << "th test case";
+ throw std::runtime_error(error_msg.str());
+ }
+ }
+
+ std::cout << "done-testing, success\n";
+}
diff --git a/challenge-253/witawayar/raku/ch-1.raku b/challenge-253/witawayar/raku/ch-1.raku
new file mode 100644
index 0000000000..c24b059bec
--- /dev/null
+++ b/challenge-253/witawayar/raku/ch-1.raku
@@ -0,0 +1,18 @@
+my &fun = -> @words, $separator { @words>>.split($separator).flat.grep(?*).list };
+
+#`{
+- Since "words" come after "separator", opted for an explicit signature
+- Split each item in words over the separator
+ - `>>` the hyper operator acts like a `map` here
+- Then flatten out the splitted strings
+- And eliminate empty ones by grepping for truthful strings
+}
+use Test;
+my @tests of Pair =
+ (("one.two.three", "four.five", "six"), ".") => ("one", "two", "three", "four", "five", "six"),
+ (("\$perl\$\$", "\$\$raku\$"), "\$") => ("perl", "raku");
+
+for @tests -> (:key(($words, $sep)), :value($expected-output)) {
+ ok fun(@$words, $sep) eqv $expected-output;
+ LAST done-testing;
+}
diff --git a/challenge-253/witawayar/raku/ch-2.raku b/challenge-253/witawayar/raku/ch-2.raku
new file mode 100644
index 0000000000..8c937cf35b
--- /dev/null
+++ b/challenge-253/witawayar/raku/ch-2.raku
@@ -0,0 +1,28 @@
+my &fun = { @^matrix.map({ $++ => $_ }).sort(-> $a, $b { $a.value.sum <=> $b.value.sum || $a.key <=> $b.key })>>.key };
+
+#`{
+- Take the positional matrix parameter implicitly
+- Decorate it with the indexes (a Schwartzian transform)
+- Sort over the number of 1s first, then the indexes
+- Undecorate to get the indexes that would sort
+}
+use Test;
+my @tests of Pair =
+ [
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]
+ ] => (2, 0, 3, 1, 4),
+ [
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+ ] => (0, 2, 3, 1);
+
+for @tests -> (:key($input), :value($expected-output)) {
+ ok fun($input) eqv $expected-output;
+ LAST done-testing;
+}