aboutsummaryrefslogtreecommitdiff
path: root/challenge-239/bruce-gray
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2023-10-22 21:01:46 +0100
committerUtil <bruce.gray@acm.org>2023-10-22 21:01:46 +0100
commit45b3d932cfdbc7355f2144da6d4276272431491d (patch)
tree753409d3950499676831e1f6c6f8073be51a32b8 /challenge-239/bruce-gray
parent21841fe883c6b814df6fb021280b0386b9a178b7 (diff)
downloadperlweeklychallenge-club-45b3d932cfdbc7355f2144da6d4276272431491d.tar.gz
perlweeklychallenge-club-45b3d932cfdbc7355f2144da6d4276272431491d.tar.bz2
perlweeklychallenge-club-45b3d932cfdbc7355f2144da6d4276272431491d.zip
Add TWC 239 solutions by Bruce Gray (Raku only).
Diffstat (limited to 'challenge-239/bruce-gray')
-rw-r--r--challenge-239/bruce-gray/raku/ch-1.raku19
-rw-r--r--challenge-239/bruce-gray/raku/ch-2.raku26
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-239/bruce-gray/raku/ch-1.raku b/challenge-239/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..243f0d02a7
--- /dev/null
+++ b/challenge-239/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,19 @@
+my &task1 = *.join eq *.join;
+
+
+# Alternate phrasings of the same solution:
+# my &task1 = { @^s1.join eq @^s2.join };
+# sub task1 ( @s1, @s2 --> Bool ) {
+# return @s1.join eq @s2.join;
+# }
+
+
+my @tests =
+ ( True , ( <ab c> , <a bc> ) ),
+ ( False , ( <ab c> , <ac b> ) ),
+ ( True , ( <ab cd e> , ('abcde',) ) ),
+;
+use Test; plan +@tests;
+for @tests -> ( Bool $expected, @in ) {
+ is task1(|@in), $expected;
+}
diff --git a/challenge-239/bruce-gray/raku/ch-2.raku b/challenge-239/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..13bb6b0036
--- /dev/null
+++ b/challenge-239/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,26 @@
+sub task2_via_Set ( Str $allowed, @s --> UInt ) {
+ my Set $a = $allowed.comb.Set;
+
+ return +grep { .comb.Set (-) $a ≡ ∅ }, @s;
+}
+
+sub task2_via_Regex ( Str $allowed, @s --> UInt ) {
+ return +grep / ^ @($allowed.comb.unique)+ $ /, @s;
+}
+
+
+my @tests =
+ ( 2, ( 'ab' , <ad bd aaab baa badab> ) ),
+ ( 7, ( 'abc' , <a b c ab ac bc abc> ) ),
+ ( 4, ( 'cad' , <cc acd b ba bac bad ac d> ) ),
+;
+my @subs =
+ :&task2_via_Set,
+ :&task2_via_Regex,
+;
+use Test; plan +@tests * +@subs;
+for @subs -> ( :key($sub_name), :value($task2) ) {
+ for @tests -> ( UInt $expected, @in ) {
+ is $task2.(|@in), $expected, "$sub_name @in[0]";
+ }
+} \ No newline at end of file