Peter’s blog ✴ Week 380 ✴ 29 June 2026
THE WEEKLY CHALLENGE
Frequencies and degrees
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
The Unicode code points given by ord('λ') for characters a-z are
97-122. What is required to calculate the reverse degree is thus
123 - ord('λ'), which will range from a = 26 to z = 1.
Add those together for each character in the string and that's its reverse degree.
Note that as in task 1 I have converted any upper case letters to lower case and removed any characters not in [a-z].
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-06-29 use utf8; # Week 380 - task 2 - Reverse degree use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; reverse_degree('bbc'); reverse_degree('racecar'); reverse_degree('zyx'); reverse_degree('supercalifragilisticexpialidocious'); sub reverse_degree { my ($string, $j, $reverse_degree); # initialise $string = shift; say qq[\nInput: '$string']; $string =~ s|[^a-z]||g; $string = lc($string); # calculate reverse degree for $j (0 .. length($string) - 1) { $reverse_degree += ($j + 1) * (123 - ord(substr($string, $j, 1))); } say qq[Output: $reverse_degree]; }
10 lines of code
Input: 'bbc' Output: 147 Input: 'racecar' Output: 560 Input: 'zyx' Output: 14 Input: 'supercalifragilisticexpialidocious' Output: 9387
Any content of this website which has been created by Peter Campbell Smith is in the public domain