diff options
| -rw-r--r-- | challenge-096/mimosinnet/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-096/mimosinnet/raku/ch-1.raku | 39 | ||||
| -rw-r--r-- | challenge-096/mimosinnet/raku/ch-2.raku | 37 |
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-096/mimosinnet/blog.txt b/challenge-096/mimosinnet/blog.txt new file mode 100644 index 0000000000..bda8cec3f8 --- /dev/null +++ b/challenge-096/mimosinnet/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/joan_mimosinnet/2021/01/perl-weekly-challenge-096---raku.html diff --git a/challenge-096/mimosinnet/raku/ch-1.raku b/challenge-096/mimosinnet/raku/ch-1.raku new file mode 100644 index 0000000000..ec426f2c35 --- /dev/null +++ b/challenge-096/mimosinnet/raku/ch-1.raku @@ -0,0 +1,39 @@ +=begin comment +Perl Weekly Challenge +https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/ + +TASK #1 › Reverse Words +Submitted by: Mohammad S Anwar +You are given a string $S. + +Write a script to reverse the order of words in the given string. The string may contain leading/trailing spaces. The string may have more than one space between words in the string. Print the result without leading/trailing spaces and there should be only one space between words. + +Example 1: +Input: $S = "The Weekly Challenge" +Output: "Challenge Weekly The" +Example 2: +Input: $S = " Perl and Raku are part of the same family " +Output: "family same the of part are Raku and Perl" + +=end comment + +#| +sub challenge( $sring ) { + return $sring.words.reverse; +} + +multi sub MAIN( Str $string ) { + say "Input: \$S = $string"; + say "Output: " ~ challenge($string); +} + +multi sub MAIN( 'challenge' ) { + MAIN("The Weekly Challenge" ); + MAIN(" Perl and Raku are part of the same family "); +} + +multi sub MAIN( 'test' ) { + use Test; + is challenge("The Weekly Challenge"), "Challenge Weekly The"; + is challenge(" Perl and Raku are part of the same family "), "family same the of part are Raku and Perl"; +} diff --git a/challenge-096/mimosinnet/raku/ch-2.raku b/challenge-096/mimosinnet/raku/ch-2.raku new file mode 100644 index 0000000000..bae2ded981 --- /dev/null +++ b/challenge-096/mimosinnet/raku/ch-2.raku @@ -0,0 +1,37 @@ +# The solution follows the Wagner-Fisher algorithm, +# see: https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm + +#! Distance +class Distance { + + has Str $.s1; + has Str $.s2; + + method distance() { + return $!s1.chars if $!s2.chars == 0; + return $!s2.chars if $!s1.chars == 0; + min ( + Distance.new( s1 => $!s1.chop, s2 => $!s2 ).distance + 1, # deletion + Distance.new( s1 => $!s1 , s2 => $!s2.chop ).distance + 1, # insertion + Distance.new( s1 => $!s1.chop, s2 => $!s2.chop ).distance + !($!s1.substr(*-1) eq $!s2.substr(*-1)).Num; # substitution cost + ); + } +} + +multi sub MAIN( Str $a, Str $b ) { + say "Input: \$S1 = \"$a\"; \$S2 = \"$b\""; + say "Output: " ~ Distance.new( s1 => $a, s2 => $b ).distance; +} + +multi sub MAIN( 'challenge' ) { + MAIN( "kitten", "sitting" ); + MAIN( "sunday", "monday" ); + MAIN( "Saturday", "Sunday" ); +} + +multi sub MAIN( 'test' ) { + use Test; + + is Distance.new( s1 => "kitten", s2 => "sitting").distance, 3; + is Distance.new( s1 => "sunday", s2 => "monday" ).distance, 2; +} |
