blob: 04355ce4572d72307a4fafb021f427012a1936a3 (
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
40
41
42
43
44
45
46
47
48
49
|
# Test: perl6 ch-2.p6
sub MAIN() {
say 'Solutions for: aabaab:';
for (palindrome('aabaab')) -> $answer {
say $answer.join(", ")
};
say "\n\nSolutions for: abbaba:";
for (palindrome('abbaba')) -> $answer {
say $answer.join(", ");
};
}
sub palindrome (Str $string){
my @letters = $string.comb;
# All possible answers
my @answers;
# Divide the answers into chunks #scalar(@letters)
loop (my $i = 0; $i < @letters.elems; $i++) {
my $possible_word = '';
# Process each chunk
loop (my $j = $i; $j < @letters.elems; $j++) {
$possible_word ~= @letters[$j];
# Make sure the word is longer than 1 char
if ($possible_word.chars > 1) {
# Check this word
if ($possible_word eq $possible_word.flip) {
# Recusive check
my $sub_string = substr($string, $j + 1);
my @palins = palindrome($sub_string);
if (@palins) {
for (@palins) -> $palin {
push @answers, [ $possible_word, @$palin ] ;
}
} else {
@answers.push([ $possible_word ]);
}
}
}
}
}
return @answers;
}
|