aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2023-07-16 15:59:39 -0500
committerUtil <bruce.gray@acm.org>2023-07-16 15:59:39 -0500
commit1771f9b0c0f3a584dc5d10845d5ea67759e4d827 (patch)
treec90502d0c307660fa1aa99d53d78a84dc7e07bc9
parente9b62f140e6c30616f4b162c85474ac45cff3841 (diff)
downloadperlweeklychallenge-club-1771f9b0c0f3a584dc5d10845d5ea67759e4d827.tar.gz
perlweeklychallenge-club-1771f9b0c0f3a584dc5d10845d5ea67759e4d827.tar.bz2
perlweeklychallenge-club-1771f9b0c0f3a584dc5d10845d5ea67759e4d827.zip
Add TWC 225 solutions by Bruce Gray (Raku only).
-rw-r--r--challenge-225/bruce-gray/raku/ch-1.raku17
-rw-r--r--challenge-225/bruce-gray/raku/ch-2.raku22
2 files changed, 39 insertions, 0 deletions
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;
+}