aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/mohammad-anwar/raku/ch-2a.p6
blob: e19c6fd7c58ef5391ec860ba9a43aa552d909add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env perl6

use Test;

is word-break("perlweeklychallenge", [ "weekly", "challenge", "perl" ]),
   [ "perl", "weekly", "challenge" ],
   "match test";

is word-break("perlandraku", [ "python", "ruby", "haskell" ]),
   0,
   "no match test";

sub word-break(Str $string, @words where .all ~~ Str) {

    my @matched = @words.grep: -> $word { $string ~~ / $word / };
    return 0 unless @matched.elems;

    my %search-order = @matched.map(
        -> $word { $string.index($word) => $word });

    return [
        %search-order.keys.sort({ $^a <=> $^b })
        .map({ %search-order{$_} })
    ];
}