aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-21 19:42:26 +0100
committerGitHub <noreply@github.com>2024-04-21 19:42:26 +0100
commit6e0b5101539f2dc9f4b46a37f6d15ba8b4637197 (patch)
treeeec74cb2608080e2e27931822cc2737cdeee263b
parent1cbdeea985ff19f0e533b5a96d6899d044e085ed (diff)
parent7202346c9e25d1f48134a05d4e66e42f87791b95 (diff)
downloadperlweeklychallenge-club-6e0b5101539f2dc9f4b46a37f6d15ba8b4637197.tar.gz
perlweeklychallenge-club-6e0b5101539f2dc9f4b46a37f6d15ba8b4637197.tar.bz2
perlweeklychallenge-club-6e0b5101539f2dc9f4b46a37f6d15ba8b4637197.zip
Merge pull request #9964 from Util/c265
Add TWC 265 solutions by Bruce Gray, in Raku only.
-rw-r--r--challenge-265/bruce-gray/raku/ch-1.raku26
-rw-r--r--challenge-265/bruce-gray/raku/ch-2.raku28
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-265/bruce-gray/raku/ch-1.raku b/challenge-265/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..751522444d
--- /dev/null
+++ b/challenge-265/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,26 @@
+sub task1 ( @ns --> UInt:_ ) {
+ my $limit = +@ns * 0.33;
+
+ return @ns.Bag.grep( *.value >= $limit ).min.?key;
+}
+# Credit: After reading wambash's code, I changed from `or return Nil`
+# to in-line the `.min.?key` using the `.?` trick, which I had not
+# known worked on the -Inf that `.min` returns on an empty list.
+# I also learned a `.nodemap` trick that, while I did not use it here,
+# I know I will have use for in the future:
+# https://docs.raku.org/routine/nodemap
+# When applied to Associatives, it will act on the values
+
+
+use Test; plan +constant @tests =
+ ( 3, (1,2,3,3,3,3,4,2) ),
+ ( 1, (1,1) ),
+ ( 1, (1,2,3) ),
+
+ ( Nil, (1,2,3,4,5) ),
+ ( Nil, (1,1,2,2,3,4,5) ),
+ ( 2, (1 ,2,2,3,4,5) ),
+;
+for @tests -> ( $expected, @in ) {
+ is task1(@in), $expected;
+}
diff --git a/challenge-265/bruce-gray/raku/ch-2.raku b/challenge-265/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..4144a82db3
--- /dev/null
+++ b/challenge-265/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,28 @@
+# I point out:
+# * You can directly `.comb(/.../)`, instead of `.comb.grep(/.../)`.
+# * You can `.lc` the whole word/string, instead of each letter.
+sub task2 ( Str $s, @ss --> Str ) {
+ my &letter_bag = *.lc.comb(/<.alpha>/).Bag;
+
+ my $lb = letter_bag($s);
+
+ my @completing_words = @ss.grep( *.&letter_bag ⊇ $lb )
+ or return '';
+
+ return @completing_words.min(*.chars);
+}
+# Credit: I changed original `(-)` to `(<=)` after reading arne-sommer's code,
+# then flipped the args and changed to Unicode `⊇`.
+
+
+use Test; plan +constant @tests =
+ ( 'accbbb' , 'aBc 11c' , <accbbb abc abbc> ),
+ ( 'baacd' , 'Da2 abc' , <abcm baacd abaadc> ),
+ ( 'bjb' , 'JB 007' , <jj bb bjb> ),
+
+ # Test that we are really returning smallest of the completing words, not just the first one.
+ ( 'accbbb' , 'aBc 11c' , <accbbbb accbbb> ),
+;
+for @tests -> ( Str $expected, Str $in_s, @in_ss ) {
+ is task2($in_s, @in_ss), $expected;
+}