forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch-1.pl
executable file
·39 lines (31 loc) · 825 Bytes
/
ch-1.pl
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
#!/usr/bin/env perl
use v5.40;
my $startsWithVowel = qr/ ^[aeiou] /ix;
sub goatLatin($sentence) {
my @words = split /\s+/, $sentence;
my $a_count = 1;
foreach my $word ( @words ) {
if ($word =~ /$startsWithVowel/) {
$word .= 'ma';
}
else {
my $first = substr($word, 0, 1);
my $rest = substr($word, 1);
$word = $rest . $first . 'ma';
}
$word .= 'a' x $a_count++;
}
return join(' ', @words);
}
sub solution($sentence) {
say 'Input: $sentence = "' . $sentence . '"';
say 'Output: "' . goatLatin($sentence) . '"';
}
say "Example 1:";
solution("I love Perl");
say "\nExample 2:";
solution("Perl and Raku are friends");
say "\nExample 3:";
solution("The Weekly Challenge");
say "\nExample 4:";
solution("Bus stop Bus goes She stays Love grows Under my umbrella");