diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-13 08:10:27 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-13 08:10:27 +0000 |
| commit | 35ffba1dc540263be3ab391ee18978e5a91f8077 (patch) | |
| tree | d03dbd48c111de50420ff304cbf978d8d6f7bc31 | |
| parent | ea8e553d31c8ded0f54369f1d0df7bf614511bab (diff) | |
| parent | a4052e8c0d68e054f931d7658fc7252c725e329b (diff) | |
| download | perlweeklychallenge-club-35ffba1dc540263be3ab391ee18978e5a91f8077.tar.gz perlweeklychallenge-club-35ffba1dc540263be3ab391ee18978e5a91f8077.tar.bz2 perlweeklychallenge-club-35ffba1dc540263be3ab391ee18978e5a91f8077.zip | |
Merge pull request #7718 from Util/c207
Add TWC 207 solutions by Bruce Gray (Raku only).
| -rw-r--r-- | challenge-207/bruce-gray/raku/ch-1.raku | 37 | ||||
| -rw-r--r-- | challenge-207/bruce-gray/raku/ch-2.raku | 20 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-207/bruce-gray/raku/ch-1.raku b/challenge-207/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..50e4a9acb8 --- /dev/null +++ b/challenge-207/bruce-gray/raku/ch-1.raku @@ -0,0 +1,37 @@ +# I usually would have phrased this as a Bool sub, +# but since the whole sub is just a match against a constant regex, +# and a Regex is a Matcher, I skipped that division. +# +# Also, I like this solution, because it easily allows +# for alternate keyboards that duplicate letters! +# +# FYI, unlike Perl, // in scalar assignment +# produces the Regex object itself, like Perl's `qr{}`. +# +# I am using `:i` to make the regex case-insensitive. +# I might have added `:ignoremark`, but that crashes on +# the ancient Rakudo on this laptop. +# +sub task1 ( @s --> Seq ) { + constant $all_in_one_row = / + :i + ^ [ <[qwertyuiop]>+ + | <[asdfghjkl]>+ + | <[zxcvbnm]>+ + ] + $ + /; + + return @s.grep: $all_in_one_row; +} + + +constant @tests = + ( <Hello Alaska Dad Peace> , <Alaska Dad> ), + ( <OMG Bye> , ().Seq ), +; +use Test; +plan +@tests; +for @tests -> ( $in, $expected ) { + is-deeply task1($in), $expected; +} diff --git a/challenge-207/bruce-gray/raku/ch-2.raku b/challenge-207/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..106e3cbb73 --- /dev/null +++ b/challenge-207/bruce-gray/raku/ch-2.raku @@ -0,0 +1,20 @@ +sub task2 ( @ns --> UInt ) { + # Most concise and clear + return +@ns.sort(-*).pairs.grep: { .value > .key }; + + # Faster, but less clear + # return @ns.sort(-*).pairs.first({ .value <= .key }).?key + # // @ns.elems; +} + + +constant @tests = + ( ( 10, 8, 5, 4, 3 ), 4 ), + ( ( 25, 8, 5, 3, 3 ), 3 ), + ( ( 2, 2, 2, 2, 2 ), 2 ), +; +use Test; +plan +@tests; +for @tests -> ( $in, $expected ) { + is task2($in), $expected; +} |
