diff options
| author | rir <rirans@comcast.net> | 2024-02-10 12:30:11 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-02-10 12:30:11 -0500 |
| commit | c78cc5a4dfa37512e6a0d45a13011906c77c806e (patch) | |
| tree | 972f31cb1e2e4cab6fb749854eb11092283b56eb | |
| parent | f4de42db83c16b8cc87f47d9bfa9eb1266c4e827 (diff) | |
| download | perlweeklychallenge-club-c78cc5a4dfa37512e6a0d45a13011906c77c806e.tar.gz perlweeklychallenge-club-c78cc5a4dfa37512e6a0d45a13011906c77c806e.tar.bz2 perlweeklychallenge-club-c78cc5a4dfa37512e6a0d45a13011906c77c806e.zip | |
255
| -rw-r--r-- | challenge-255/0rir/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-255/0rir/raku/ch-2.raku | 74 |
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-255/0rir/raku/ch-1.raku b/challenge-255/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..9912231f7c --- /dev/null +++ b/challenge-255/0rir/raku/ch-1.raku @@ -0,0 +1,50 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +255-1: Odd Character Submitted by: Mohammad Sajid Anwar +You are given two strings, $s and $t. The string $t is generated using + the shuffled characters of the string $s with an additional character. + +Write a script to find the additional character in the string $t.. + +Example 1 +Input: $s = "Perl" $t = "Preel" +Output: "e" +Example 2 +Input: $s = "Weekly" $t = "Weeakly" +Output: "a" +Example 3 +Input: $s = "Box" $t = "Boxy" +Output: "y" +=end comment + +my @Test = + "Perl", "Preel", "e", + "Weekly", "Weeakly", "a", + "Box", "Boxy", "y", +; + +plan @Test ÷ 3 + 3; + +sub func( Any:D $long where *.chars > 1, + Any:D $short where *.chars == $long.chars -1 --> Str) { + return ($long.comb.Bag ∖ $short.comb.Bag).keys[0]; +} + +for @Test -> $short, $long, $exp { + is func($long, $short), $exp, "$exp <- $long, $short"; +} +dies-ok { func( 'abc', 'abc') }, 'Bad data'; +dies-ok { func( 'abc', 'abcd') }, 'Bad data'; +dies-ok { func( 'a', 'a') }, 'Bad data'; + +done-testing; +my $s = 'abcdefghijjk'; +my $t = 'abcdefghijjjk'; +qq{\nInput: \$s = "$s" \$t = "$t"\nOutput: &func($t,$s)}.say; + +exit; + diff --git a/challenge-255/0rir/raku/ch-2.raku b/challenge-255/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..192920b11a --- /dev/null +++ b/challenge-255/0rir/raku/ch-2.raku @@ -0,0 +1,74 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +255-2: Most Frequent Word Submitted by: Mohammad Sajid Anwar +Given a paragraph $p and a banned word $w. + +Write a script to return the most frequent word that is not banned. + +Example 1 +Input: $p = "Joe hit a ball, the hit ball flew far after it was hit." + $w = "hit" +Output: "ball" + +The banned word "hit" occurs 3 times. +The other word "ball" occurs 2 times. +Example 2 +Input: $p = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge." + $w = "the" +Output: "Perl" + +The banned word "the" occurs 3 times. +The other word "Perl" occurs 2 times. +=end comment + +my @Test = + "Joe hit a ball, the hit ball flew far after it was hit.", + "hit", "ball", + "Perl and Raku belong to the same family. Perl is the most " + ~ "popular language in the weekly challenge.", + "the", "Perl", + "Dogs like cats, cats like mice, mice like cheese.", + 'Dogs', 'like', + "Pearl bought a pearl at a seafood shop.", # NOTE: case issues + 'seafood', 'a', + "Dogs like cats, cats like mice, mice like cheese.", + 'cheese', 'like', + "a a a b b c d e", 'e', 'a', +; + +my @Die = + "Dogs like cats, cats like mice, mice like cheese.", + 'like', 'cats', +; + +plan (@Die + @Test) ÷ 3; + +my regex word { <:L>+ }; + +sub func( $text, $ban-word ) { + my Bag $word = Bag.new( + $text.comb( / <word> /).grep( { ! / $ban-word/})); + my $candidate = ($word.grep: *.value == $word.values.max )».key; + die "Multiple candidates" if +@$candidate > 1; + $candidate; # if @candi -Naive use of array of size 1 as *[0] +} + +for @Test -> $pp, $ban, $exp { + is func($pp, $ban), $exp, "$exp <- $ban <-$pp"; +} +for @Die -> $pp, $ban, $exp { + dies-ok { func($pp, $ban) }, "DIES <- $ban <-$pp"; +} + +done-testing; +my $t = "Joe hit a ball, the hit ball flew far after it was hit."; +my $b = "hit"; + +say qq{\nInput: \$p = "$t"\n \$w = "$b"\nOutput: "&func( $t,$b)"}; + +exit; + |
