Reverse Sum
with Raku

by Arne Sommer

Reverse Sum with Raku

[405] Published 5. July 2026.

This is my response to The Weekly Challenge #380.

#380.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 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

The letter y is an English vowel, some of the time. See e.g. The Truth About 'Y': It's Mostly a Vowel for more info. The fourth example in our challenge, rythm, (wrongly) treats it as consonant when it really should not, so I'll do the same.

File: sum-of-frequencies
#! /usr/bin/env raku

unit sub MAIN ($str where $str ~~ /^ <[a .. z]>+ $/,
               :v(:$verbose));

my $bag = $str.comb.Bag;

my @vowels     = $bag<a e i o u>;
my @consonants = $bag<b c d f g h j k l m n p q r s t v w x y z>;

my $max-vowel       = @vowels.max;
my $max-consonants  = @consonants.max;

if $verbose
{
  say ": Vovel Count: @vowels[] -> $max-vowel";
  say ": Consonant Count: @vowels[] -> $max-consonants";
}

say $max-vowel + $max-consonants;

[3] Ensure English lowercase letters only.

[6] Count the occurene of each letter in the word, as a Bag.

See docs.raku.org/routine/Bag for more information about Bag.

[8] A list with the occurence of the given vowels. Note that the vowels are not returned, just a list of integers.

[9] Ditto for the consonants. (We could have started with a list of all the letters (a..z) and removed the vowels, to avoid the long list of consonants.)

[11] The highest vowel frequency.

[12] Ditto for the consonants.

[20] Add the numbers from [11] and [12] and print the result.

Running it:

$ ./sum-of-frequencies banana
5

$ ./sum-of-frequencies teestett
7

$ ./sum-of-frequencies aeiouuaa
3

$ ./sum-of-frequencies rhythm
2

$ ./sum-of-frequencies x
1

Looking good.

With verbose mode:

$ ./sum-of-frequencies -v banana
: Vovel Count: 3 0 0 0 0 -> 3
: Consonant Count: 3 0 0 0 0 -> 2
5

$ ./sum-of-frequencies -v teestett
: Vovel Count: 0 3 0 0 0 -> 3
: Consonant Count: 0 3 0 0 0 -> 4
7

$ ./sum-of-frequencies -v aeiouuaa
: Vovel Count: 3 1 1 1 2 -> 3
: Consonant Count: 3 1 1 1 2 -> 0
3

$ ./sum-of-frequencies -v rhythm
: Vovel Count: 0 0 0 0 0 -> 0
: Consonant Count: 0 0 0 0 0 -> 2
2

$ ./sum-of-frequencies -v x
: Vovel Count: 0 0 0 0 0 -> 0
: Consonant Count: 0 0 0 0 0 -> 1
1

#380.2 Reverse Degree 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
File: reverse-degree
#! /usr/bin/env raku

unit sub MAIN ($str where $str ~~ /^ <[a .. z]>+ $/,
               :v(:$verbose));

my %h = ('z' ... 'a').map({ state $i = 1;  $_ => $i++});

my $total = 0;

for 1 .. $str.chars -> $i
{
  my $letter = $str.substr($i -1, 1);
  my $sum    = $i * %h{$letter};

  $total += $sum;
 
  say ": Pos:$i Char:'$letter' Val:%h{$letter} Sum:$sum"
    if $verbose;
}

say $total;

[3] The challenge specifies only lowercase English letters in the input.

[6] Construct a hash with mappings from letter to value, starting with z => 1 and ending with a => 26. Note the state variable used as a counter for the right hand side.

See docs.raku.org/syntax/state for more information about the variable declarator state.

[8] The result will end up here.

[10] Iterate over the positions of the letters in the string, starting at 1.

[12] Get the character at that position. (Note the -1 as indices are zero based.)

[13] Get the sum for this letter, the position ($i) multiplied with the occurence (%h{$letter}).

[15] Add that sum to the result.

[21] Print the final result.

Running it:

$ ./reverse-degree z
1

$ ./reverse-degree a
26

$ ./reverse-degree bbc
147

$ ./reverse-degree racecar
560

$ ./reverse-degree zyx
14

Looking good.

With verbose mode:

$ ./reverse-degree -v z
: Pos:1 Char:'z' Val:1 Sum:1
1

$ ./reverse-degree -v a
: Pos:1 Char:'a' Val:26 Sum:26
26

$ ./reverse-degree -v bbc
: Pos:1 Char:'b' Val:25 Sum:25
: Pos:2 Char:'b' Val:25 Sum:50
: Pos:3 Char:'c' Val:24 Sum:72
147

$ ./reverse-degree -v racecar
: Pos:1 Char:'r' Val:9 Sum:9
: Pos:2 Char:'a' Val:26 Sum:52
: Pos:3 Char:'c' Val:24 Sum:72
: Pos:4 Char:'e' Val:22 Sum:88
: Pos:5 Char:'c' Val:24 Sum:120
: Pos:6 Char:'a' Val:26 Sum:156
: Pos:7 Char:'r' Val:9 Sum:63
560

$ ./reverse-degree -v zyx
: Pos:1 Char:'z' Val:1 Sum:1
: Pos:2 Char:'y' Val:2 Sum:4
: Pos:3 Char:'x' Val:3 Sum:9
14

And that's it.