Perl Weekly Challenge 380.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 380.

Task 1: Sum of Frequencies

Submitted by: Mohammad Sajid Anwar
You are given a string consisting of English letters.

Write a script to find the vowel and consonant with maximum frequency.
Return the sum of two frequencies.

Example 1
Input: $str = "banana"
Output: 5

Vowel: "a" appears 3 times.
Consonant: "n" appears 2 times, "b" appears 1 time.

Max frequency of vowel: 3
Max frequency of consonant: 2

Example 2
Input: $str = "teestett"
Output: 7

Vowel: "e" appears 3 times.
Consonant: "t" appears 4 times, "s" appears 1 time.

Max frequency of vowel: 3
Max frequency of consonant: 4

Example 3
Input: $str = "aeiouuaa"
Output: 3

Vowel: "a" appears 3 times, "u" 2 times, "e", "i", "o" 1 time each.
Consonant: None.

Max frequency of vowel: 3
Max frequency of consonant: 0

Example 4
Input: $str = "rhythm"
Output: 2

Vowel: None
Consonant: "h" appears 2 times, "r", "y", "t", "m" 1 time each.

Max frequency of vowel: 0
Max frequency of consonant: 2

Example 5
Input: $str = "x"
Output: 1

Vowel: None
Consonant: "x" appears 1 time.

Max frequency of vowel: 0
Max frequency of consonant: 1


I use a hash to count the frequencies of each letter. I use max from List::Util to get the maximum frequency of vowels. Then I delete the entries for vowels and find the maximum over the remaining counts, which corresponds to consonants. The result is their sum. The code takes a 1.5-liner.

Examples:

perl -MList::Util=max -E '
@v=qw(a e i o u);for(@ARGV){my%c;$c{$_}++for split"",$_;
$v=max @c{@v};delete@c{@v}; say"$_ -> ",$v+max values%c;}
' banana teestett aeiouuaa rhythm x

Results:

banana -> 5
teestett -> 7
aeiouuaa -> 3
rhythm -> 2
x -> 1

The full code is:

 1  # Perl weekly challenge 380
 2  # Task 1:  Sum of Frequencies
 3  #
 4  # See https://wlmb.github.io/2026/06/29/PWC380/#task-1-sum-of-frequencies
 5  use v5.36;
 6  use feature qw(try);
 7  use List::Util qw(max);
 8  die <<~"FIN" unless @ARGV;
 9      Usage: $0 S0 S1...
10      to sum the frequencies of the most frequent vowel and
11      consonant in string Sn.
12      FIN
13  my @vowels = qw(a e i o u);
14  for(@ARGV){
15      my $lc = lc $_;
16      try {
17          die "Expected English letters only: $_" unless $lc=~/^[a-z]*$/;
18          my %count;
19          @count{'a'..'z'}=(0) x 26;
20          $count{$_}++ for split "", $lc;
21          my $vowels = max @count{@vowels};
22          delete @count{@vowels};
23          my $consonants=max values %count;
24          my $sum = $vowels + $consonants;
25          say "$_ -> $sum";
26      }
27      catch($e){ warn $e; }
28  }

Example:

./ch-1.pl banana teestett aeiouuaa rhythm x

Results:

banana -> 5
teestett -> 7
aeiouuaa -> 3
rhythm -> 2
x -> 1

Task 2: Reverse Degree

Submitted by: Mohammad Sajid Anwar
You are given a string.

Write a script to find the reverse degree of the given
string.

For each character, multiply its position in the reversed
alphabet (‘a’ = 26, ‘b’ = 25, …, ‘z’ = 1) with its position
in the string. Sum these products for all characters in the
string to get the reverse degree.

Example 1
Input: $str = "z"
Output: 1

Reverse alphabet value of "z" is 1.
Position 1: 1 x 1
Sum of product: 1

Example 2
Input: $str = "a"
Output: 26

Reverse alphabet value of "a" is 26.
Position 1: 1 x 26
Sum of product: 26

Example 3
Input: $str = "bbc"
Output: 147

Reverse alphabet value of "b" is 25 and "c" is 24.
Position 1: 1 x 25
Position 2: 2 x 25
Position 3: 3 x 24
Sum of product: 25 + 50 + 72 => 147

Example 4
Input: $str = "racecar"
Output: 560

Reverse alphabet value of "r" is 9, "a" is 26, "c" is 24 and "e" is 24.
Position 1: 1 x 9
Position 2: 2 x 26
Position 3: 3 x 24
Position 4: 4 x 22
Position 5: 5 x 24
Position 6: 6 x 26
Position 7: 7 x 9
Sum of product: 9 + 52 + 72 + 88 + 120 + 156 + 63

Example 5
Input: $str = "zyx"
Output: 14

Reverse alphabet value of "z" is 1, "y" is 2 and "x" is 3.
Position 1: 1 x 1
Position 2: 2 x 2
Position 3: 3 x 3
Sum of product: 1 + 4 + 9

I just follow the instructions, keeping an eye for off-by-one errors. The result takes a half-liner.

Examples:

perl -MList::Util=sum -E '
$z=ord("z")+1;
for(@ARGV){my$p;say"$_ -> ", sum map{++$p*($z-ord $_)}split"";}
' z a bbc racecar zyx

Results:

z -> 1
a -> 26
bbc -> 147
racecar -> 560
zyx -> 14

The full code is:

 1  # Perl weekly challenge 380
 2  # Task 2:  Reverse Degree
 3  #
 4  # See https://wlmb.github.io/2026/06/29/PWC380/#task-2-reverse-degree
 5  use v5.36;
 6  use feature qw(try);
 7  use List::Util qw(sum0);
 8  die <<~"FIN" unless @ARGV;
 9      Usage: $0 S0 S1...
10      to find the reverse degree of string Sn
11      FIN
12  my $z=ord("z")+1;
13  for(@ARGV){
14      try {
15          my $lc=lc $_;
16          die "Expected English letters only: $_" unless $lc=~/^[a-z]*$/;
17          my $position;
18          say"$_ -> ", sum0 map { ++$position * ($z-ord $_)} split"";
19      }
20      catch($e){warn $e;}
21  }

Example:

./ch-2.pl z a bbc racecar zyx

Results:

z -> 1
a -> 26
bbc -> 147
racecar -> 560
zyx -> 14

/;

Written on June 29, 2026