diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-21 21:19:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-21 21:19:29 +0000 |
| commit | 6e1728468bff842c9cfe1642883cbcce9fcbb04d (patch) | |
| tree | 9c20afdde8ffbdaafc05a892af73100600d5d4db | |
| parent | d9158621438166c0d4510ffe001bb339107bd6e8 (diff) | |
| parent | c040e950295d9ef9a3a952b8319ba074a51b1513 (diff) | |
| download | perlweeklychallenge-club-6e1728468bff842c9cfe1642883cbcce9fcbb04d.tar.gz perlweeklychallenge-club-6e1728468bff842c9cfe1642883cbcce9fcbb04d.tar.bz2 perlweeklychallenge-club-6e1728468bff842c9cfe1642883cbcce9fcbb04d.zip | |
Merge pull request #9437 from mustafaaydn/challenge_252
Solutions for the 252nd challenge
| -rw-r--r-- | challenge-252/witawayar/cpp/ch-1.cpp | 38 | ||||
| -rw-r--r-- | challenge-252/witawayar/cpp/ch-2.cpp | 28 | ||||
| -rw-r--r-- | challenge-252/witawayar/raku/ch-1.raku | 22 | ||||
| -rw-r--r-- | challenge-252/witawayar/raku/ch-2.raku | 14 |
4 files changed, 102 insertions, 0 deletions
diff --git a/challenge-252/witawayar/cpp/ch-1.cpp b/challenge-252/witawayar/cpp/ch-1.cpp new file mode 100644 index 0000000000..2d06260474 --- /dev/null +++ b/challenge-252/witawayar/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include <algorithm> // transform +#include <iostream> // cout +#include <numeric> // accumulate +#include <sstream> // stringstream +#include <vector> + +int fun(std::vector<int> const& ints) { + std::vector<int> out; + const std::size_t N = ints.size(); + std::vector<std::size_t> indexes(N); + std::iota(indexes.begin(), indexes.end(), 1u); + std::transform(ints.cbegin(), ints.cend(), + indexes.cbegin(), std::back_inserter(out), + [&](const int& value, const std::size_t& index) { + return N % index == 0 ? value * value : 0; }); + + return std::accumulate(out.cbegin(), out.cend(), 0); +} + +int main(void) { + // Tests + std::vector<std::pair<std::vector<int>, int>> tests = { + {{1, 2, 3, 4}, 21}, + {{2, 7, 1, 19, 18, 3}, 63}, + }; + + 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-252/witawayar/cpp/ch-2.cpp b/challenge-252/witawayar/cpp/ch-2.cpp new file mode 100644 index 0000000000..91b103757b --- /dev/null +++ b/challenge-252/witawayar/cpp/ch-2.cpp @@ -0,0 +1,28 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-2.cpp +#include <iostream> // cout +#include <numeric> // accumulate +#include <sstream> // stringstream +#include <vector> + +std::vector<int> fun(int const& n) { + std::vector<int> range(n); + std::iota(range.begin(), range.end(), -(n / 2)); + return range; +} + +int main(void) { + // Tests + std::vector<unsigned> tests{5, 3, 1}; + + for (const unsigned& input : tests) { + std::vector<int> got = fun(input); + int sum = std::accumulate(got.cbegin(), got.cend(), 0); + if (sum != 0) { + std::stringstream error_msg; + error_msg << "Expected " << 0 << ", got " << sum; + throw std::runtime_error(error_msg.str()); + } + } + + std::cout << "done-testing, success\n"; +} diff --git a/challenge-252/witawayar/raku/ch-1.raku b/challenge-252/witawayar/raku/ch-1.raku new file mode 100644 index 0000000000..c79a1fe59f --- /dev/null +++ b/challenge-252/witawayar/raku/ch-1.raku @@ -0,0 +1,22 @@ +my &fun = { @^ints.kv.map({ $^value ** 2 if +@ints %% $^index.succ }).sum } + +#`{ +- Take an array of things via the implicit signature @^ints +- Reach its "keys" and "values" via `.kv` + - Keys are the 0-based indexes, values are, well, the values themselves + - This gives a 2N length sequence +- We can `.map` the interleaved key-value sequence via an implicit signature + - Lexicographically, index < value, so when taking 2 things at a time (since arity == 2), + `.map` will assign the first one (key) to $^index, second one (value) to $^value +- Then accumulate value squared if index's "succ"essor (index + 1) is a divisor of @^ints' length +} + +use Test; +my @tests of Pair = + (1, 2, 3, 4) => 21, + (2, 7, 1, 19, 18, 3) => 63; + +for @tests -> (:key($input), :value($expected-output)) { + ok fun($input) eqv $expected-output; + LAST done-testing; +} diff --git a/challenge-252/witawayar/raku/ch-2.raku b/challenge-252/witawayar/raku/ch-2.raku new file mode 100644 index 0000000000..238397366d --- /dev/null +++ b/challenge-252/witawayar/raku/ch-2.raku @@ -0,0 +1,14 @@ +my &fun = { list -$_ .. $_ given $^n div 2 } + +#`{ +- Take a scalar via the implicit signature $^n +- Floor-divide it by 2, and the range between minus that and itself satisfies the condition +} + +use Test; +my @tests of UInt = 5, 3, 1; + +for @tests -> $n { + ok fun($n).sum == 0; + LAST done-testing; +} |
