The Weekly Challenge 380

Sum of Frequencies and Reverse Degree

Task 1: Sum of Frequencies

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 Output

Input: $str = "banana" Output: 5 (Vowel "a" appears 3 times, consonant "n" appears 2 times) Input: $str = "teestett" Output: 7 (Vowel "e" appears 3 times, consonant "t" appears 4 times)

Logic

Convert the string to lowercase and filter out non-alphabetic characters. Count the occurrences of vowels (a, e, i, o, u) and consonants separately, find the maximum frequency in each category, and return their sum.

Perl Solution

ch-1.pl

sub sum_of_frequencies ($str) { my %vowels = map { $_ => 1 } qw(a e i o u); my %vowel_counts; my %consonant_counts; for my $char (split //, lc($str)) { next unless $char =~ /[a-z]/; if (exists $vowels{$char}) { $vowel_counts{$char}++; } else { $consonant_counts{$char}++; } } my $max_vowel = 0; for my $count (values %vowel_counts) { $max_vowel = $count if $count > $max_vowel; } my $max_consonant = 0; for my $count (values %consonant_counts) { $max_consonant = $count if $count > $max_consonant; } return $max_vowel + $max_consonant; }

Python Solution

ch-1.py

def sum_of_frequencies(s: str) -> int: vowels_set = set("aeiou") vowel_counts: Counter[str] = Counter() consonant_counts: Counter[str] = Counter() for char in s.lower(): if char.isalpha() and "a" <= char <= "z": if char in vowels_set: vowel_counts[char] += 1 else: consonant_counts[char] += 1 max_vowel = max(vowel_counts.values()) if vowel_counts else 0 max_consonant = max(consonant_counts.values()) if consonant_counts else 0 return max_vowel + max_consonant

Task 2: Reverse Degree

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

Example Output

Input: $str = "bbc" Output: 147 ('b' = 25 * 1 + 'b' = 25 * 2 + 'c' = 24 * 3)

Logic

For each character in the string, compute its value in the reversed alphabet ('a'=26, 'b'=25, ..., 'z'=1). Multiply this value by its 1-based index in the string and sum these products for all characters.

Perl Solution

ch-2.pl

sub reverse_degree ($str) { my $sum = 0; my @chars = split //, lc($str); for my $i (0 .. $#chars) { my $char = $chars[$i]; next unless $char =~ /[a-z]/; my $value = 26 - (ord($char) - ord('a')); my $position = $i + 1; $sum += $value * $position; } return $sum; }

Python Solution

ch-2.py

def reverse_degree(s: str) -> int: total = 0 for i, char in enumerate(s.lower()): if char.isalpha() and "a" <= char <= "z": value = 26 - (ord(char) - ord("a")) position = i + 1 total += value * position return total