Perl Weekly Challenge 311.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 311.
Task 1: Upper Lower
Submitted by: Mohammad Sajid Anwar
You are given a string consists 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 seems to be and ideal case for the tr
operator, which yields a
simple half-liner
Examples
perl -E '
for(@ARGV){say "$_ -> ", tr/a-zA-Z/A-Za-z/r;}
' pERl rakU PyThOn
Results:
pERl -> PerL
rakU -> RAKu
PyThOn -> pYtHoN
The full code is essentially identical.
1 # Perl weekly challenge 311
2 # Task 1: Upper Lower
3 #
4 # See https://wlmb.github.io/2025/03/03/PWC311/#task-1-upper-lower
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 S1 S2...
8 to change upper<->lower case in the strings Sn
9 FIN
10 for(@ARGV){
11 say "$_ -> ", tr/a-zA-Z/A-Za-z/r;
12 }
Example:
./ch-1.pl pERl rakU PyThOn
Results:
pERl -> PerL
rakU -> RAKu
PyThOn -> pYtHoN
Task 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"
I assume the parameters are in @ARGV as lengths followed by the
corresponding strings. I use the length to build a regular expression
which I use to match and split the string. Each substring is splitted
into digits, summed, and joined to make the new string. The process is iterated
until the result has a small enough length. I use the new for-list
syntax to iterate over pair of arguments. I use sum0
from
List::Util
to perform the sums. The result fits a 1.5 liner.
Examples:
perl -MList::Util=sum0 -E '
for my($l, $s)(@ARGV){$n=$s;$n=join "",map{sum0 split ""}$n=~/(\d{1,$l})/g while
length$n>$l;say "$l $s -> $n"}
' 3 111122333 2 1222312 4 100012121001
Results:
3 111122333 -> 359
2 1222312 -> 76
4 100012121001 -> 162
The corresponding full code is:
1 # Perl weekly challenge 311
2 # Task 2: Group Digit Sum
3 #
4 # See https://wlmb.github.io/2025/03/03/PWC311/#task-2-group-digit-sum
5 use v5.36;
6 use List::Util qw(sum0);
7 die <<~"FIN" unless @ARGV && @ARGV%2==0;
8 Usage: $0 L0 N0 L1 N1...
9 to recursively sum the digits of groups of digits of length Ln
10 taken from the numbers Nn until a fixed point is found.
11 FIN
12 for my($length, $string)(@ARGV){
13 warn("Only digits allowed in string: $string"), next unless $string=~/^\d+$/;
14 my $new=$string;
15 $new = join "", map {sum0 split ""} $new =~ /(\d{1,$length})/g
16 while length $new > $length;
17 say "length $length, string $string -> $new"
18 }
Example:
./ch-2.pl 3 111122333 2 1222312 4 100012121001
Results:
length 3, string 111122333 -> 359
length 2, string 1222312 -> 76
length 4, string 100012121001 -> 162
/;