diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2020-06-08 11:12:49 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2020-06-08 11:12:49 +0200 |
| commit | c4708467abd49747d2a660d44bcbecf07d0c52b7 (patch) | |
| tree | 396c0eb55dd24dfc69302935916d2e1a6ae57091 | |
| parent | f2c3c84295ef3b02c765dcf5b72777892c30b9da (diff) | |
| download | perlweeklychallenge-club-c4708467abd49747d2a660d44bcbecf07d0c52b7.tar.gz perlweeklychallenge-club-c4708467abd49747d2a660d44bcbecf07d0c52b7.tar.bz2 perlweeklychallenge-club-c4708467abd49747d2a660d44bcbecf07d0c52b7.zip | |
Task 2 done.
| -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; +} |
