aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/e-choroba/perl/ch-2.pl
blob: 82edaf113ca01bb1fa78755aeadd6b4cb568831f (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/perl
use warnings;
use strict;

sub _word_break {
    my ($string, $results, @words) = @_;
    return $results unless @words;

    for my $i (0 .. $#words) {
        my $word = $words[$i];
        next unless 0 == index $string, $word;

        my $r = _word_break(substr($string, length $word),
                            [@$results, $word],
                            @words[ grep $_ != $i, 0 .. $#words ]);
        return $r if $r;
    }
    return 0
}

sub word_break {
    my ($string, @words) = @_;
    return _word_break($string, [], @words)
}

use Test::More tests => 4;

is_deeply word_break(qw( perlweeklychallenge weekly challenge perl )),
          [qw[ perl weekly challenge ]];

is_deeply word_break(qw( perlandraku python ruby haskell )),
          0;

is_deeply word_break(qw( aabaa a ab aa )),
          [qw[ a ab aa ]];

is_deeply word_break(qw( abababa a bab aba )),
          [qw[ a bab aba ]];