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
50
|
# Test: perl6 ch-2.p6
multi MAIN { MAIN(2) };
multi MAIN(Int $size) {
# Generate the possible combinations
my @vowels;
push @vowels, 'a', 'e', 'i', 'o', 'u'
for (1 .. $size);
my @combos = @vowels.combinations: $size;
# Check each combination
my @solutions;
for @combos.unique -> @combo {
push @solutions, @combo.join
if (valid-combination(@combo));
}
# Print the solutions
.say for @solutions.unique.sort;
}
# IS valid combination
sub valid-combination(@word) {
# Faster than a regex
loop (my $i = 0; $i < @word.elems - 1; $i++) {
return 0 unless
_check-letters(@word, $i, 'a', ['e','i']) &&
_check-letters(@word, $i, 'e', ['i']) &&
_check-letters(@word, $i, 'i', ['a', 'e', 'o', 'u']) &&
_check-letters(@word, $i, 'o', ['a', 'u']) &&
_check-letters(@word, $i, 'u', ['o', 'e']);
}
return 1;
}
# Check the folowing letters
sub _check-letters(@word, Int $i, $letter, @checks) {
my $valid = True;
if (@word[$i] eq $letter) {
$valid = False;
for (@checks) -> $check {
$valid = True
if (@word[$i + 1] eq $check);
}
}
return $valid;
}
|