diff options
| -rw-r--r-- | challenge-254/witawayar/cpp/ch-1.cpp | 37 | ||||
| -rw-r--r-- | challenge-254/witawayar/cpp/ch-2.cpp | 51 | ||||
| -rw-r--r-- | challenge-254/witawayar/raku/ch-1.raku | 24 | ||||
| -rw-r--r-- | challenge-254/witawayar/raku/ch-2.raku | 25 |
4 files changed, 137 insertions, 0 deletions
diff --git a/challenge-254/witawayar/cpp/ch-1.cpp b/challenge-254/witawayar/cpp/ch-1.cpp new file mode 100644 index 0000000000..f08e21c8c6 --- /dev/null +++ b/challenge-254/witawayar/cpp/ch-1.cpp @@ -0,0 +1,37 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-1.cpp +#include <cmath> // abs, round, cbrt +#include <iostream> +#include <sstream> // form the error message +#include <vector> + +static constexpr double TOLERANCE = 1E-6; + +bool fun(int n) { + double cube_root = std::cbrt(n); + return std::abs(cube_root - std::round(cube_root)) < TOLERANCE; +} + +int main() { + // Tests + std::vector<std::pair<int, int>> tests = { + {0, true}, + {1, true}, + {-343, true}, + {27, true}, + {122, false}, + {6, false}, + {777, false} + }; + + 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-254/witawayar/cpp/ch-2.cpp b/challenge-254/witawayar/cpp/ch-2.cpp new file mode 100644 index 0000000000..9adcfdd9e6 --- /dev/null +++ b/challenge-254/witawayar/cpp/ch-2.cpp @@ -0,0 +1,51 @@ +// g++ -Wall -Wextra -Wpedantic -std=c++17 cpp/ch-2.cpp +#include <algorithm> // copy_if, reverse, transfomr +#include <iostream> +#include <sstream> // form the error message +#include <vector> + +static constexpr double TOLERANCE = 1E-6; + +bool is_vowel(char c) { c = std::tolower(c); return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } + +std::string fun(const std::string_view str) { + std::vector<char> vowels; + std::copy_if(str.cbegin(), str.cend(), std::back_inserter(vowels), is_vowel); + std::reverse(vowels.begin(), vowels.end()); + + std::string new_str(str.size(), '\0'); + auto vow_it = vowels.begin(); + std::transform(str.cbegin(), str.cend(), + new_str.begin(), [&vow_it](char c) { + return + is_vowel(c) + ? ('A' < c) && (c < 'Z') + ? std::toupper(*vow_it++) + : std::tolower(*vow_it++) + : c; + }); + + return new_str; +} + +int main() { + // Tests + std::vector<std::pair<std::string_view, std::string_view>> tests = { + {"Raku", "Ruka"}, + {"Perl", "Perl"}, + {"Julia", "Jaliu"}, + {"Uiua", "Auiu"}, + }; + + for (const auto& [input, expected_output] : tests) { + auto 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-254/witawayar/raku/ch-1.raku b/challenge-254/witawayar/raku/ch-1.raku new file mode 100644 index 0000000000..8ae0cbce6c --- /dev/null +++ b/challenge-254/witawayar/raku/ch-1.raku @@ -0,0 +1,24 @@ +my &fun = { $_ =~= .round given $^n.abs ** (1 / 3) }; + +#`{ +- Take implicitly the scalar $^n +- Take its cube root after absoluted + - Raku gives NaN for negative values to fractional powers + - e.g., (-5) ** (3 / 2) ?==? sqrt(-125) +- Check if it is approximately equal to its rounded form + - If so, it was an integer "modulo" floating point issues +} +use Test; +my @tests of Pair = + 0 => True, + 1 => True, + -343 => True, + 27 => True, + 122 => False, + 6 => False, + 777 => False; + +for @tests -> (:key($input), :value($expected-output)) { + ok fun($input) eqv $expected-output; + LAST done-testing; +} diff --git a/challenge-254/witawayar/raku/ch-2.raku b/challenge-254/witawayar/raku/ch-2.raku new file mode 100644 index 0000000000..bef4c4d8d9 --- /dev/null +++ b/challenge-254/witawayar/raku/ch-2.raku @@ -0,0 +1,25 @@ +my &fun = { + my @vowels-reversed = $^str.comb(/:i <[aeiou]> /).reverse; + $str.comb.map({ /:i <[aeiou]> / + ?? @vowels-reversed[$++]."{$_ ~~ "A".."Z" ?? "uc" !! "lc"}"() + !! $_ }).join +}; +#`{ +- Take implicitly the scalar $^str +- Extract the vowels and reverse +- Map the characters: vowel? get from reversed list; otherwise as is + - While getting from the reversed list, care for current's case + - Call `&uc` or `&lc` dependingly, and Raku allows for a method + call to be made through a string, so... (e.g., `$value."method"()` is valid) +} +use Test; +my @tests of Pair = + "Raku" => "Ruka", + "Perl" => "Perl", + "Julia" => "Jaliu", + "Uiua" => "Auiu"; + +for @tests -> (:key($input), :value($expected-output)) { + ok fun($input) eqv $expected-output; + LAST done-testing; +} |
