From a4052e8c0d68e054f931d7658fc7252c725e329b Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 12 Mar 2023 13:33:49 -0500 Subject: Add TWC 207 solutions by Bruce Gray (Raku only). --- challenge-207/bruce-gray/raku/ch-1.raku | 37 +++++++++++++++++++++++++++++++++ challenge-207/bruce-gray/raku/ch-2.raku | 20 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-207/bruce-gray/raku/ch-1.raku create mode 100644 challenge-207/bruce-gray/raku/ch-2.raku 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 = + ( , ), + ( , ().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; +} -- cgit