Perl Weekly Challenge 365.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 365.
Task 1: Alphabet Index Digit Sum
Submitted by: Mohammad Sajid Anwar
You are given a string $str consisting of lowercase English
letters, and an integer $k.
Write a script to convert a lowercase string into numbers
using alphabet positions (a=1 … z=26), concatenate them to
form an integer, then compute the sum of its digits repeatedly
$k times, returning the final value.
Example 1
Input: $str = "abc", $k = 1
Output: 6
Conversion: a = 1, b = 2, c = 3 -> 123
Digit sum: 1 + 2 + 3 = 6

Example 2
Input: $str = "az", $k = 2
Output: 9
Conversion: a = 1, z = 26 -> 126
1st sum: 1 + 2 + 6 = 9
2nd sum: 9

Example 3
Input: $str = "cat", $k = 1
Output: 6
Conversion: c = 3, a = 1, t = 20 -> 3120
Digit sum: 3 + 1 + 2 + 0 = 6

Example 4
Input: $str = "dog", $k = 2
Output: 8
Conversion: d = 4, o = 15, g = 7 -> 4157
1st sum: 4 + 1 + 5 + 7 = 17
2nd sum: 1 + 7 = 8

Example 5
Input: $str = "perl", $k = 3
Output: 6
Conversion: p = 16, e = 5, r = 18, l = 12 -> 1651812
1st sum: 1 + 6 + 5 + 1 + 8 + 1 + 2 = 24
2nd sum: 2+4 = 6
3rd sum: 6

I use a hash as a translation table from letters to numbers. I split
the string into letters and join the corresponding numbers. Then I
repeatedly split the number into digits and add them with sum from
List::Util. The code takes a two-liner.
Examples:
perl -MList::Util=sum -E '
@n{a..z}=(1..26);for my($s,$k)(@ARGV){$x=join"",map$n{$_},split "",$s;
$x=sum split "",$x for(1..$k);say"$s, $k -> $x"}
' abc 1 az 2 cat 1 dog 2 perl 3
Results:
abc, 1 -> 6
az, 2 -> 9
cat, 1 -> 6
dog, 2 -> 8
perl, 3 -> 6
The full code is:
1 # Perl weekly challenge 365
2 # Task 1: Alphabet Index Digit Sum
3 #
4 # See https://wlmb.github.io/2026/03/16/PWC365/#task-1-alphabet-index-digit-sum
5 use v5.36;
6 use feature qw(try);
7 use List::Util qw(sum);
8 my %letter_to_number;
9 @letter_to_number{"a".."z"}=(1..26);
10 die <<~"FIN" unless @ARGV && @ARGV%2==0;
11 Usage: $0 S0 k0 S1 k1...
12 to convert the string Sn to digits and sum them kn times.
13 FIN
14 for my($str, $k)(@ARGV){
15 try {
16 die "Only lower case letter allowed: $str" unless $str=~/^[a-z]+$/;
17 my $x= join "", map {$letter_to_number{$_}} split "",$str;
18 $x=sum split "",$x for(1..$k);
19 say"$str, $k -> $x";
20 }
21 catch($e) { warn $e; }
22 }
Example:
./ch-1.pl abc 1 az 2 cat 1 dog 2 perl 3
Results:
abc, 1 -> 6
az, 2 -> 9
cat, 1 -> 6
dog, 2 -> 8
perl, 3 -> 6
Task 2: Valid Token Counter
Submitted by: Mohammad Sajid Anwar
You are given a sentence.
Write a script to split the given sentence into
space-separated tokens and count how many are valid words. A
token is valid if it contains no digits, has at most one
hyphen surrounded by lowercase letters, and at most one
punctuation mark (!, ., ,) appearing only at the end.
Example 1
Input: $str = "cat and dog"
Output: 3
Tokens: "cat", "and", "dog"

Example 2
Input: $str = "a-b c! d,e"
Output: 2
Tokens: "a-b", "c!", "d,e"
"a-b" -> valid (one hyphen between letters)
"c!" -> valid (punctuation at end)
"d,e" -> invalid (punctuation not at end)

Example 3
Input: $str = "hello-world! this is fun"
Output: 4
Tokens: "hello-world!", "this", "is", "fun"
All satisfy the rules.

Example 4
Input: $str = "ab- cd-ef gh- ij!"
Output: 2
Tokens: "ab-", "cd-ef", "gh-", "ij!"
"ab-" -> invalid (hyphen not surrounded by letters)
"cd-ef" -> valid
"gh-" -> invalid
"ij!" -> valid

Example 5
Input: $str = "wow! a-b-c nice."
Output: 2
Tokens: "wow!", "a-b-c", "nice."
"wow!" -> valid
"a-b-c" -> invalid (more than one hyphen)
"nice." -> valid
I split the input into space separated tokens, filter them with a regular expression that recognizes word characters optionally followed by a hyphen and more word characters and optionally followed by a punctuation character, and finally, count the tokens that get across the filter. The result fits a one-liner.
Examples:
perl -E '
$r="[[:alpha:]]+";for(@ARGV){say "$_ -> ", 0+grep /^$r(-$r)?[\.,!]?$/,split " "}
' "cat and dog" "a-b c! d,e" "hello-world! this is fun" \
"ab- cd-ef gh- ij!" "wow! a-b-c nice."
Results:
cat and dog -> 3
a-b c! d,e -> 2
hello-world! this is fun -> 4
ab- cd-ef gh- ij! -> 2
wow! a-b-c nice. -> 2
I only allowed the punctuation characters mentioned explicitly in the problem statement.
The full code is:
1 # Perl weekly challenge 365
2 # Task 2: Valid Token Counter
3 #
4 # See https://wlmb.github.io/2026/03/16/PWC365/#task-2-valid-token-counter
5 use v5.36;
6 die <<~ "FIN" unless @ARGV;
7 Usage: $0 S0 S1...
8 to count valid space-separated tokens in the strings Sn
9 FIN
10 for(@ARGV){
11 say "$_ -> ",
12 0+
13 grep {/^[[:alpha:]]+(-[[:alpha:]]+)?[\.,!]?$/}
14 split " "
15 }
Example:
./ch-2.pl "cat and dog" "a-b c! d,e" "hello-world! this is fun" \
"ab- cd-ef gh- ij!" "wow! a-b-c nice."
Results:
cat and dog -> 3
a-b c! d,e -> 2
hello-world! this is fun -> 4
ab- cd-ef gh- ij! -> 2
wow! a-b-c nice. -> 2
/;