From 1771f9b0c0f3a584dc5d10845d5ea67759e4d827 Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 16 Jul 2023 15:59:39 -0500 Subject: Add TWC 225 solutions by Bruce Gray (Raku only). --- challenge-225/bruce-gray/raku/ch-1.raku | 17 +++++++++++++++++ challenge-225/bruce-gray/raku/ch-2.raku | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge-225/bruce-gray/raku/ch-1.raku create mode 100644 challenge-225/bruce-gray/raku/ch-2.raku diff --git a/challenge-225/bruce-gray/raku/ch-1.raku b/challenge-225/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..d0f9b529cb --- /dev/null +++ b/challenge-225/bruce-gray/raku/ch-1.raku @@ -0,0 +1,17 @@ +sub task1 (@sentences --> UInt) { + return @sentences.map( *.words.elems ).max; +} + +# Shorter: +# my &task1 = *».words».elems.max; + + +my @tests = + ( 8, ( 'Perl and Raku belong to the same family.', 'I love Perl.', 'The Perl and Raku Conference.', ) ), + ( 7, ( 'The Weekly Challenge.', 'Python is the most popular guest language.', 'Team PWC has over 300 members.' ) ), +; +use Test; +plan +@tests; +for @tests -> ( $expected, @sentences ) { + is task1(@sentences), $expected; +} diff --git a/challenge-225/bruce-gray/raku/ch-2.raku b/challenge-225/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..c017f23950 --- /dev/null +++ b/challenge-225/bruce-gray/raku/ch-2.raku @@ -0,0 +1,22 @@ +sub abs_diff { abs $^x - $^y } +sub partial_sum_0 { 0, |[\+] @^a.head(*-1) } + +sub task2 ( Int @ns --> Array ) { + + my @left = partial_sum_0(@ns); + my @right = partial_sum_0(@ns.reverse).reverse; + + return @left »[&abs_diff]« @right; +} + + +my @tests = + ( (10, 4, 8, 3) , (15, 1, 11, 22) ), + ( (1,) , (0,) ), + ( (1, 2, 3, 4, 5) , (14, 11, 6, 1, 10) ), +; +use Test; +plan +@tests; +for @tests -> ( @in, @expected ) { + is-deeply task2( Array[Int].new(@in) ), @expected.Array; +} -- cgit