Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #365 (“Alphabet Index Digit Sum” and “Valid Token Counter”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge

The Weekly Challenge for the week of 2026-03-16 through 2026-03-22 is #365. The tasks for challenge #365 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 365-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
Expected output: 6

Example 2 input: $str = "az", $k = 2
Expected output: 9

Example 3 input: $str = "cat", $k = 1
Expected output: 6

Example 4 input: $str = "dog", $k = 2
Expected output: 8

Example 5 input: $str = "perl", $k = 3
Expected output: 6

While I found it tempting to use recursion, ultimately I found it overkill for this problem, so I used a for loop instead which splits and sums a sum $k times.

Robbie Hatley's Perl Solution to The Weekly Challenge 365-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 365-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"
Expected output: 3

Example 2 input: $str = "a-b c! d,e"
Expected output: 2

Example 3 input: $str = "hello-world! this is fun"
Expected output: 4

Example 4 input: $str = "ab- cd-ef gh- ij!"
Expected output: 2

Example 5 input: $str = "wow! a-b-c nice."
Expected output: 2

I start by fully decomposing any extended grapheme clusters (so that any combining marks become separate Unicode codepoints); then I strip-out all combining marks (so that lower-case letters can easily be recognized as such); then I split the sentence on whitespace to tokens; then I scrutinizing all tokens and count only those which do not disobey the rules; then I return my final count.

Robbie Hatley's Perl Solution to The Weekly Challenge 365-2

That's it for challenge 365; see you on challenge 366!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)