Task1
First one is a math trick that we use to find out if a number is a power of 3 or not:
#!/usr/bin/env perl
use strict;
use warnings;
sub three_power{
$_[0] == int($_[0]**(1/3))**3
}
printf "%d\n",three_power(27);
printf "%d\n",three_power(0);
printf "%d\n",three_power(6);
Task2
We reverse the list of vowels and replace them in the string, and capitalize the first letter:
#!/usr/bin/env perl
use strict;
use warnings;
sub reverse_vowels{
my ($str) = @_;
my @vowels = $str =~ /[aeiou]/gi;
$str =~ s/([aeiou])/pop @vowels/egi;
ucfirst $str
}
printf "%s\n",reverse_vowels('Raku');
printf "%s\n",reverse_vowels('Perl');
printf "%s\n",reverse_vowels('Julia');
printf "%s\n",reverse_vowels('Uiua');
No comments:
Post a Comment