diff options
| -rw-r--r-- | challenge-254/0rir/raku/ch-1.raku | 11 | ||||
| -rw-r--r-- | challenge-255/0rir/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-255/0rir/raku/ch-2.raku | 74 |
3 files changed, 127 insertions, 8 deletions
diff --git a/challenge-254/0rir/raku/ch-1.raku b/challenge-254/0rir/raku/ch-1.raku index 7081fb7e89..fba96c8382 100644 --- a/challenge-254/0rir/raku/ch-1.raku +++ b/challenge-254/0rir/raku/ch-1.raku @@ -16,7 +16,7 @@ Output: true constant \Part-power = 40; # partitioning power constant \Part = 3**Part-power; # " value -constant \Test-to-power = 100; # rough range for tests +constant \Test-to-power = 1000; # rough range for tests constant @test-powers = 3, * × 3 … 3**Test-to-power; # passing test values constant @standard = gather { # create standard for look up in the partition @@ -30,17 +30,12 @@ constant @standard = gather { # create standard for look up in the partition sub power3 ( Int $i is copy -->Bool) { if $i ≤ @standard[*-1] { - if $i == @standard.any { return True if $i == @standard.any; - } else { return False; - } - } else { + } my $shrunk = $i div @standard[*-1]; # shrink by a partition return False if $i ÷ @standard[*-1] ≠ $shrunk; # not integer - return power3 $shrunk; - } - die "reached but did not grasp"; + power3 $shrunk; } my @Test = 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; + |
