diff options
| -rw-r--r-- | challenge-278/0rir/raku/ch-1.raku | 69 | ||||
| -rw-r--r-- | challenge-278/0rir/raku/ch-2.raku | 70 |
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-278/0rir/raku/ch-1.raku b/challenge-278/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..955a3755ad --- /dev/null +++ b/challenge-278/0rir/raku/ch-1.raku @@ -0,0 +1,69 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # π¦ β
βͺβ©ββββ β‘ β’ Β«β€ Β» β΄ +use v6.d; +use Test; + +=begin comment +278 1: Sort String Submitted by: Mohammad Sajid Anwar + + XXX my translation XXX + +Given a shuffled string, $str, return the ordered string. The shuffled +string is unshuffled by using the position number appended to each word. + +Example 1 +Input: $str = "and2 Raku3 cousins5 Perl1 are4" +Output: "Perl and Raku are cousins" +Example 2 +Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7" +Output: "Python is the most popular guest language" +Example 3 +Input: $str = "Challenge3 The1 Weekly2" +Output: "The Weekly Challenge" +=end comment + +my @Test = + #in /exp + "and2 Raku3 cousins5 Perl1 are4", + "Perl and Raku are cousins", + "guest6 Python1 most4 the3 popular5 is2 language7", + "Python is the most popular guest language", + "Challenge3 The1 Weekly2", + "The Weekly Challenge", + ( "More1 than2 the3 average4 butterfly5 by6 ceiling,7" + ~ " range8 and9 load10 capacity.11" + #).split(' ').pick(*).join(' '), + ).&shuffle, + "More than the average butterfly by ceiling, range and load capacity." +; +my @Die = Str, '', '1', '1a', 'a', 'ab', '1a1', 'a1a', 'a2', 'a1b3'; + +plan @Test Γ· 2 + @Die;; + +sub shuffle( Str $str -->Str) { $str.split(' ').pick(*).join(' ') } + +multi task( Any:D $in where * eq '' ) { die 'Error: Zero length.' } +multi task( Any:D $in -->Str) { + my @ordered-word; + for $in.words { + if so $_ ~~ / ^ $<w> = [\D+] $<k> = [\d+] $ / { + @ordered-word[$<k>.Int - 1] = $<w>.Str; + } else { + die qq{Error: "$_" is malformed.}; + } + } + die 'Error: Bad sequencing.' unless @ordered-word.all.defined; + @ordered-word.join: ' '; +} + +for @Test -> $in, $exp { is task($in), $exp, "$exp <- $in"; } + +for @Die -> $in { dies-ok { task $in }, "Died <- " ~ ( $in // "(Str)"); } + +done-testing; + +my $str = ( "More1 than2 the3 average4 butterfly5 by6 ceiling,7 range8" + ~ " and9 by10 carrying11 phasers.12").&shuffle; + +say "\nInput: \$str = $str.raku()\nOutput: &task( $str).raku()"; + diff --git a/challenge-278/0rir/raku/ch-2.raku b/challenge-278/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..06875cb3fd --- /dev/null +++ b/challenge-278/0rir/raku/ch-2.raku @@ -0,0 +1,70 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # π¦ β
βͺβ©ββββ β‘ β’ Β«β€ Β» β΄ +use v6.d; +use Test; + +=begin comment +278 2: Reverse Word Submitted by: Mohammad Sajid Anwar + +Given a word, $word and a character, $char. Replace the substring up +to and including $char with its characters sorted alphabetically. If +the $char doesnβt exist then DON'T do anything. + +Example 1 +Input: $str = "challenge", $char = "e" +Ouput: "acehllnge" +Example 2 +Input: $str = "programming", $char = "a" +Ouput: "agoprrmming" +Example 3 +Input: $str = "champion", $char = "b" +Ouput: "champion" + +=end comment + +my @Test = +# word char exp + 'a', 'x', 'a', + 'a', 'a', 'a', + 'ab', 'a', 'ab', + 'ba', 'a', 'ab', + 'abc', 'a', 'abc', + 'bac', 'a', 'abc', + 'abc', 'b', 'abc', + 'abc', 'c', 'abc', + "challenge", "e", "acehllnge", + "programming", "a", "agoprrmming", + "champion", "b", "champion", + 'edcba', 'a', 'abcde', +; + +my @Die = Str, 'a', + Any, 'a', + '', 'a'; + +plan @Test Γ· 3 + @Die Γ· 2; + + +multi task( Any:D $w where * ~~ / ^ <:L>+ $ / , + Any:D $char where *.chars == 1 -->Str:D) { + my @a = $w.comb; + my $k = ( return $w ) R// @a.first( $char, :k); + my @head = @a[0..$k].sort: *.fc; + return @head.join unless @a[$k + 1] :exists; + #@head.join ~ @a[$k+1 .. *].join; + ( @head, @a[$k+1 .. *]).flat.join; +} + +for @Test -> $w, $c, $exp { + is task($w, $c), $exp, "$exp.raku() <<- $c.raku() <β~ $w.raku()"; +} +for @Die -> $w, $c { + dies-ok { task $w, $c}, "Dead <<- $c.raku() <β~ $w.raku()"; +} +done-testing; + +my $str = 'Anotherusefulquiz'; +my $char = 'l'; +say qq{\nInput: \$str = $str.raku(), \$char = $char.raku()\n} + ~ qq{Output: &task( $str, $char).raku()}; + |
