aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-11 23:56:20 +0000
committerGitHub <noreply@github.com>2024-02-11 23:56:20 +0000
commit104246971d1badf3a4e3c49f04f041d6d8289caf (patch)
tree746b08d024fcc2275658afce8030e06164025a7c
parent03ef16a34b5fe68f8721f523ff90ac658b568a5d (diff)
parent74e789ca9ca2858181dc2462831be058fdf6df41 (diff)
downloadperlweeklychallenge-club-104246971d1badf3a4e3c49f04f041d6d8289caf.tar.gz
perlweeklychallenge-club-104246971d1badf3a4e3c49f04f041d6d8289caf.tar.bz2
perlweeklychallenge-club-104246971d1badf3a4e3c49f04f041d6d8289caf.zip
Merge pull request #9562 from Util/c255
Add TWC 255 solutions by Bruce Gray, in Raku only.
-rw-r--r--challenge-255/bruce-gray/raku/ch-1.raku22
-rw-r--r--challenge-255/bruce-gray/raku/ch-2.raku36
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-255/bruce-gray/raku/ch-1.raku b/challenge-255/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..943fcdab68
--- /dev/null
+++ b/challenge-255/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,22 @@
+subset Char of Str where *.chars == 1;
+
+sub task1 ( Str $s, Str $t --> Char ) {
+ my @ts = $t, $s;
+
+ warn "\$t ('$t') should be 1 char more than \$s ('$s')"
+ if 1 != [-] @ts».chars;
+ my Bag $r = [(-)] @ts».comb».Bag;
+
+ warn if $r.elems !== 1;
+
+ return ~$r;
+}
+
+
+my @tests = map { %( <expected s t> Z=> .list ) },
+ < e Perl Preel >,
+ < a Weekly Weeakly >,
+ < y Box Boxy >,
+;
+use Test; plan +@tests;
+is task1(|.<s t>), .<expected>, .<s t> for @tests;
diff --git a/challenge-255/bruce-gray/raku/ch-2.raku b/challenge-255/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..49528dcbef
--- /dev/null
+++ b/challenge-255/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,36 @@
+sub task2_BagHash ( $banned, $paragraph --> Str ) {
+ die unless $banned.words == 1;
+ my BagHash $b = $paragraph.comb(/\w+/).BagHash;
+
+ $b{$banned}:delete;
+
+ return $b.max(*.value).key;
+}
+sub task2_Grep ( $banned, $paragraph --> Str ) {
+ die unless $banned.words == 1;
+
+ return $paragraph
+ .comb(/\w+/)
+ .grep(*.fc ne $banned.fc)
+ .Bag
+ .max(*.value)
+ .key;
+}
+# Note: the task does not specify what to do for ties, and I did not use .maxpairs to find them.
+
+
+my @tests = map { %( <expected in_banned in_paragraph> Z=> .list ) },
+ ( 'ball', 'hit', 'Joe hit a ball, the hit ball flew far after it was hit.' ),
+ ( 'Perl', 'the', 'Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.' ),
+;
+my @subs =
+ :&task2_BagHash,
+ :&task2_Grep,
+;
+use Test; plan +@tests * +@subs;
+for @subs -> ( :key($sub_name), :value(&task2) ) {
+ for @tests {
+ is task2(|.<in_banned in_paragraph>), .<expected>,
+ "$sub_name\t{.<in_banned>}";
+ }
+}