diff options
| -rw-r--r-- | challenge-064/luca-ferrari/raku/ch-2.p6 | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-064/luca-ferrari/raku/ch-2.p6 b/challenge-064/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..9cb3352004 --- /dev/null +++ b/challenge-064/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,43 @@ +#!raku + +# You are given a string $S and an array of words @W. +# +# Write a script to find out if $S can be split into sequence of one or more words as in the given @W. +# +# Print the all the words if found otherwise print 0. +# Example 1: +# +# Input: +# +# $S = "perlweeklychallenge" +# @W = ("weekly", "challenge", "perl") +# +# Output: +# +# "perl", "weekly", "challenge" + + +sub MAIN( Str $S? = 'perlweeklychallenge', @W? = [ "weekly", "challenge", "perl" ] ){ + + my $string = $S; + my @found-words; + my $redo = True; + + + while ( $redo ) { + $redo = False; # if no one match, skip the next loop + for @W -> $part { + # if the current string begins with this token, + # mark as found and remove from the string + if $string ~~ / ^ $part / { + @found-words.push: $part; + $string ~~ s/ ^ $part //; + $redo = True; + } + } + } + + # all done + "In the string $S I found the words { @found-words.join( ',' ) } ".say if @found-words; + '0'.say if ! @found-words; +} |
