diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-24 11:59:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-24 11:59:56 +0100 |
| commit | 6bcede03c7bc556acbd7786ec83b7687eaae9252 (patch) | |
| tree | cf65775cd487fbe789205fbf097d7c6f6cb051de | |
| parent | 1133deffe417e44310790a0d58a11fe9312e2e5a (diff) | |
| parent | 6b02c07600c43bdb9a10ab6cf161e029b278b52e (diff) | |
| download | perlweeklychallenge-club-6bcede03c7bc556acbd7786ec83b7687eaae9252.tar.gz perlweeklychallenge-club-6bcede03c7bc556acbd7786ec83b7687eaae9252.tar.bz2 perlweeklychallenge-club-6bcede03c7bc556acbd7786ec83b7687eaae9252.zip | |
Merge pull request #12403 from 0rir/work
331
| -rw-r--r-- | challenge-331/0rir/raku/ch-1.raku | 56 | ||||
| -rw-r--r-- | challenge-331/0rir/raku/ch-2.raku | 82 |
2 files changed, 138 insertions, 0 deletions
diff --git a/challenge-331/0rir/raku/ch-1.raku b/challenge-331/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..a71a230e53 --- /dev/null +++ b/challenge-331/0rir/raku/ch-1.raku @@ -0,0 +1,56 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.d; +use Test; + +=begin comment +331-1: Last Word +Submitted by: Mohammad Sajid Anwar +You are given a string. + +Write a script to find the length of last word in the given string. + +Example 1 +Input: $str = "The Weekly Challenge" +Output: 9 + +Example 2 +Input: $str = " Hello World " +Output: 5 + +Example 3 +Input: $str = "Let's begin the fun" +Output: 3 +=end comment + +my @Test = + # in exp + "The Weekly Challenge", 9, + " Hello World ", 5, + "Let's begin the fun", 3, + "Let's begin the fun...", 3, + qq{ ' " ... " - + @ .}, 0, + qq{ Let's " ... " - + @ .}, 1, + qq{ Let's " ... " - + @ .so}, 2, + "", 0, + " . ", 0, + "'", 0, +; +plan +@Test ÷ 2; + +my \non-word = regex { [\W | _ ]+ }; # some definition of non-word characters + +sub task( Str:D $a -->Int) { + my @a = $a.split( non-word, :skip-empty); + return 0 unless @a; + @a.tail.chars; +} + +for @Test -> $in, $exp, { + is task( $in), $exp, "{$exp // $exp.^name()} <- $in"; +} +done-testing; + +my $str = "Let's begin the fun now..."; +say qq{\nInput: \$str = "$str"\nOutput: }, task $str; + diff --git a/challenge-331/0rir/raku/ch-2.raku b/challenge-331/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..412560c780 --- /dev/null +++ b/challenge-331/0rir/raku/ch-2.raku @@ -0,0 +1,82 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +INIT $*RAT-OVERFLOW = FatRat; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +331-Task 2: Buddy Strings +Submitted by: Mohammad Sajid Anwar +You are given two strings, source and target. + +Write a script to find out if the given strings are Buddy Strings. + +If swapping of a letter in one string make them same as the other then they are `Buddy Strings`. + +Example 1 +Input: $source = "fuck" + $target = "fcuk" +Output: true + +The swapping of 'u' with 'c' makes it buddy strings. + +Example 2 +Input: $source = "love" + $target = "love" +Output: false + +Example 3 +Input: $source = "fodo" + $target = "food" +Output: true + +Example 4 +Input: $source = "feed" + $target = "feed" +Output: true +=end comment + +my @Test = + { source => "duck", target => "dcuk", exp => True, + },{ + source => "love", target => "love", exp => False, + },{ + source => "feed", target => "feed", exp => True, + },{ + source => "", target => "", exp => False, + },{ + source => "l", target => "l", exp => False, + },{ + source => "al", target => "al", exp => False, + },{ + source => "al", target => "la", exp => True, + },{ + source => "ll", target => "ll", exp => True, + },{ + source => "leeko", target => "eleki", exp => False, + },{ + source => "eek", target => "ek", exp => False, + },{ + source => "eeek", target => "eeek", exp => True, + } +; + +plan +@Test ÷ 1; + +sub task( Str:D $src, Str:D $trg -->Bool:D) { + return False if $src.chars < 2 or $trg.chars ≠ $src.chars; + my @trg = $trg.comb; + my @a = [Z=>] $src.comb, @trg; + my $dif = +@a.grep: { .key ne .value }; + given $dif { + when 2 { True } + when 0 { so @trg.BagHash.values.any > 1 } + default { False } + } +} +for @Test -> %in { + is task( %in<source>, %in<target>), + %in<exp>, "%in<exp> <- %in<source> ∘∘ %in<target>"; +} +done-testing; |
