diff options
| author | Util <bruce.gray@acm.org> | 2024-02-11 17:46:08 -0600 |
|---|---|---|
| committer | Util <bruce.gray@acm.org> | 2024-02-11 17:46:08 -0600 |
| commit | 74e789ca9ca2858181dc2462831be058fdf6df41 (patch) | |
| tree | f46d65ea36825c2ac6f9ce80feafb7079395bef0 | |
| parent | 34fa48cf4b02c81fd6ed544498bde068aca981a2 (diff) | |
| download | perlweeklychallenge-club-74e789ca9ca2858181dc2462831be058fdf6df41.tar.gz perlweeklychallenge-club-74e789ca9ca2858181dc2462831be058fdf6df41.tar.bz2 perlweeklychallenge-club-74e789ca9ca2858181dc2462831be058fdf6df41.zip | |
Add TWC 255 solutions by Bruce Gray, in Raku only.
| -rw-r--r-- | challenge-255/bruce-gray/raku/ch-1.raku | 22 | ||||
| -rw-r--r-- | challenge-255/bruce-gray/raku/ch-2.raku | 36 |
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>}"; + } +} |
