Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #311 ("Upper Lower" and "Group Digit Sum")
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:
The Weekly Challenge for the week of 2025-03-03 through 2025-03-09 is #311.
The tasks for challenge #311 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 311-1: Upper Lower Submitted by: Mohammad Sajid Anwar You are given a string consisting of english letters only. Write a script to convert lower case to upper and upper case to lower in the given string. Example #1: Input: $str = "pERl" Output: "PerL" Example #2: Input: $str = "rakU" Output: "RAKu" Example #3: Input: $str = "PyThOn" Output: "pYtHoN"
This can be easily done with a one-liner using Perl's "transpose" operator, "tr", aka "y":
y/[a-zA-Z]/[A-Za-z]/,print while <>

Robbie Hatley's Perl Solution to The Weekly Challenge 311-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 311-2: Group Digit Sum Submitted by: Mohammad Sajid Anwar You are given a string, $str, made up of digits, and an integer, $int, which is less than the length of the given string. Write a script to divide the given string into consecutive groups of size $int (plus one for leftovers if any). Then sum the digits of each group, and concatenate all group sums to create a new string. If the length of the new string is less than or equal to the given integer then return the new string, otherwise continue the process. Example 1 Input: $str = "111122333", $int = 3 Output: "359" Step 1: "111", "122", "333" => "359" Example 2 Input: $str = "1222312", $int = 2 Output: "76" Step 1: "12", "22", "31", "2" => "3442" Step 2: "34", "42" => "76" Example 3 Input: $str = "100012121001", $int = 4 Output: "162" Step 1: "1000", "1212", "1001" => "162"
The "...otherwise continue the process" in the Problem Description is a big clue that recursion will be useful here, so I'll make a recursive subroutine that performs the described summing and concatenation steps, then "continues the process" (by recursing) until the length of the string is no more than the given integer.

Robbie Hatley's Perl Solution to The Weekly Challenge 311-2
That's it for challenge 311; see you on challenge 312!
Comments
Post a Comment