/
ch-2.pl
executable file
·88 lines (82 loc) · 1.9 KB
/
ch-2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/#TASK2
#
# Task 2: Reverse Vowels
# ======================
#
# You are given a string, $s.
#
# Write a script to reverse all the vowels (a, e, i, o, u) in the given string.
#
## Example 1
##
## Input: $s = "Raku"
## Output: "Ruka"
#
## Example 2
##
## Input: $s = "Perl"
## Output: "Perl"
#
## Example 3
##
## Input: $s = "Julia"
## Output: "Jaliu"
#
## Example 4
##
## Input: $s = "Uiua"
## Output: "Auiu"
#
############################################################
##
## discussion
##
############################################################
#
# Let's first turn the string into an array of characters. Then
# let's collect the position of all vowels (and whether or not
# they were uppercase). Then we can walk through all vowels and
# write them to their new location in the correct (uppwer/lower)
# case.
use strict;
use warnings;
reverse_vowels("Raku");
reverse_vowels("Perl");
reverse_vowels("Julia");
reverse_vowels("Uiua");
sub reverse_vowels {
my $s = shift;
print "Input: \"$s\"\n";
my @chars = split //, $s;
my @vowels = ();
my @indices = ();
foreach my $i (0..$#chars) {
if(is_vowel($chars[$i])) {
push @vowels, [ $i, $chars[$i] ];
unshift @indices, [ $i, is_upper($chars[$i]) ];
}
}
foreach my $j (0..$#vowels) {
my ($old_index, $char, $is_upper) = @{ $vowels[$j] };
my ($new_index, $is_upper) = @{ $indices[$j] };
my $new_char = $char;
if($is_upper) {
$new_char = uc($new_char);
} else {
$new_char = lc($new_char);
}
$chars[$new_index] = $new_char;
}
$s = join("", @chars);
print "Output: \"$s\"\n";
}
sub is_vowel {
my $char = shift;
my $vowels = { "a" => 1, "e" => 1, "i" => 1, "o" => 1, "u" => 1 };
return $vowels->{lc($char)};
}
sub is_upper {
my $char = shift;
return $char eq uc($char);
}