Perl Weekly Challenge 378.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 378.

Task 1: Second Largest Digit

Submitted by: Mohammad Sajid Anwar
You are given an alphanumeric string.

Write a script to find the second largest distinct digit in
the given string. Return -1 if none found.

Example 1
Input: $str = "aaaaa77777"
Output: -1

The only digit in the given string is 7 and there is no
second digit.

Example 2
Input: $str = "abcde" Output: -1
No numerical digits in the given string.

Example 3
Input: $str = "9zero8eight7seven9"
Output: 8

Example 4
Input: $str = "xyz9876543210"
Output: 8

Example 5
Input: $str = "4abc4def2ghi8jkl2"
Output: 4


I can split every character, grep the digits, filter the unique (from List::Util) ones, sort them in descending order and choose the second if it exists. The result fits a one-liner.

Examples:

perl -MList::Util=uniq -E '
for(@ARGV){say "$_ -> ", (sort {$b<=>$a}uniq grep{/[0-9]/} split "")[1]//-1}
' aaaaa77777 abcde 9zero8eight7seven9 xyz9876543210 4abc4def2ghi8jkl2

Results:

aaaaa77777 -> -1
abcde -> -1
9zero8eight7seven9 -> 8
xyz9876543210 -> 8
4abc4def2ghi8jkl2 -> 4

The full code is:

 1  # Perl weekly challenge 378
 2  # Task 1:  Second Largest Digit
 3  #
 4  # See https://wlmb.github.io/2026/06/15/PWC378/#task-1-second-largest-digit
 5  use v5.36;
 6  use List::Util qw(uniq);
 7  die <<~"FIN" unless @ARGV;
 8      Usage: $0 S0 S1...
 9      to find the second largest digit in string Sn
10      FIN
11  for(@ARGV){
12      say "$_ -> ",
13          (
14           sort {$b<=>$a} # reverse sort
15           uniq           # unique digits
16           grep{/[0-9]/}  # only keep digits
17           split ""       # split in characters
18          )               # list of sorted unique digits
19          [1]             # take second element
20          //-1            # or -1 if not defined
21  }

Example:

./ch-1.pl aaaaa77777 abcde 9zero8eight7seven9 xyz9876543210 4abc4def2ghi8jkl2

Results:

aaaaa77777 -> -1
abcde -> -1
9zero8eight7seven9 -> 8
xyz9876543210 -> 8
4abc4def2ghi8jkl2 -> 4

Task 2: Sum of Words

Submitted by: Mohammad Sajid Anwar

You are given three strings consisting of lower case English
letters ‘a’ to ‘j’ only. The letter value of a = 0, b = 1, c
= 3, etc.

Write a script to find if sum of first two strings return
the third string.

Example 1
Input: $str1 = "acb", $str2 = "cba", $str3 = "cdb"
Output: true

$str1 = "acb" = 021
$str2 = "cba" = 210
$str3 = "cdb" = 231
$str1 + $str2 = $str3

Example 2
Input: $str1 = "aab", $str2 = "aac", $str3 = "ad"
Output: true

$str1 = "aab" = 001
$str2 = "aac" = 002
$str3 = "ad"  = 03

Example 3
Input: $str1 = "bc", $str2 = "je", $str3 = "jg"
Output: false

$str1 = "bc" = 12
$str2 = "je" = 94
$str3 = "jg" = 96

Example 4
Input: $str1 = "a", $str2 = "aaaa", $str3 = "aa"
Output: true

$str1 = "a"    = 0
$str2 = "aaaa" = 0000
$str3 = "aa"   = 00

Example 5
Input: $str1 = "c", $str2 = "d", $str3 = "h"
Output: false

$str1 = "c" = 2
$str2 = "d" = 3
$str3 = "h" = 7

Example 6
Input: $str1 = "gfi", $str2 = "hbf", $str3 = "bdhd"
Output: true

$str1 =  "gfi" =  658
$str2 =  "hbf" =  715
$str3 = "bdhd" = 1373

I assume the input is given as a single space separated string. I split into the three strings, tr (translate) their characters into digits, and compare the sum of the first two resulting numbers with the third. The result fits a one-liner.

Examples:

perl -E '
for(@ARGV){@x=map {tr/a-j/0-9/r} split " "; say "$_ -> ", $x[2]==$x[0]+$x[1]?"T":"F"}
' "acb cba cdb" "aab aac ad" "bc je jg" \
  "a aaaa aa"   "c d h"      "gfi hbf bdhd"

Results:

acb cba cdb -> T
aab aac ad -> T
bc je jg -> F
a aaaa aa -> T
c d h -> F
gfi hbf bdhd -> T

The full code is

 1  # Perl weekly challenge 378
 2  # Task 2:  Sum of Words
 3  #
 4  # See https://wlmb.github.io/2026/06/15/PWC378/#task-2-sum-of-words
 5  use v5.36;
 6  use feature qw(try);
 7  die <<~"FIN" unless @ARGV;
 8      Usage: $0 S0 S1...
 9      to see if the sum of the numbers associated
10      to the first two space separated substrings
11      of Sn sum to the third. The first letters are
12      mapped to the first digits.
13      FIN
14  for(@ARGV){
15      try {
16          die "Only a-j are valid characters: $_ " unless /^[a-j\s]*$/;
17          my @words = split " ";
18          die "Expected three space separated strings: $_" unless @words==3;
19          my @numbers = map {tr/a-j/0-9/r} @words;
20          my $result =  $numbers[2]==$numbers[0]+$numbers[1]?"True":"False";
21          say "$_ -> $result";
22      }
23      catch($e){ warn $e; }
24  }
25  

Example:

./ch-2.pl "acb cba cdb" "aab aac ad" "bc je jg" \
          "a aaaa aa"   "c d h"      "gfi hbf bdhd"

Results:

acb cba cdb -> True
aab aac ad -> True
bc je jg -> False
a aaaa aa -> True
c d h -> False
gfi hbf bdhd -> True

/;

Written on June 15, 2026