Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #358 (“Max Str Value” and “Encrypted String”)
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-01-26 through 2026-02-01 is #358. The tasks for challenge #358 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 358-1: Max Str Value
Submitted by: Mohammad Sajid Anwar
You are given an array of alphanumeric string, @strings. Write a
script to find the max "value" of alphanumeric string in the
given array which is the numeric representation of the string if
it consists of digits only, otherwise the length of the string.
Example #1:
Input: @strings = ("123", "45", "6")
Output: 123
"123" -> 123
"45" -> 45
"6" -> 6
Example #2:
Input: @strings = ("abc", "de", "fghi")
Output: 4
"abc" -> 3
"de" -> 2
"fghi" -> 4
Example #3:
Input: @strings = ("0012", "99", "a1b2c")
Output: 99
"0012" -> 12
"99" -> 99
"a1b2c" -> 5
Example #4:
Input: @strings = ("x", "10", "xyz", "007")
Output: 10
"x" -> 1
"xyz" -> 3
"007" -> 7
"10" -> 10
Example #5:
Input: @strings = ("hello123", "2026", "perl")
Output: 2026
"hello123" -> 8
"perl" -> 4
"2026" -> 2026
This can be solved by simply calculating the "value" (as defined in the problem description) of each string and keeping track of the maximum value seen so far.
Robbie Hatley's Perl Solution to The Weekly Challenge 358-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 358-2: Encrypted String Submitted by: Mohammad Sajid Anwar You are given a string $str and an integer $int. Write a script to encrypt the string using this algorithm: for each character $char in $str, replace $char with the $int th character after $char in the alphabet, wrapping if needed and return the encrypted string. Example #1: Input: $str = "abc", $int = 1 Output: "bcd" Example #2: Input: $str = "xyz", $int = 2 Output: "zab" Example #3: Input: $str = "abc", $int = 27 Output: "bcd" Example #4: Input: $str = "hello", $int = 5 Output: "mjqqt" Example #5: Input: $str = "perl", $int = 26 Output: "perl"
This is a "rotation" variant of a Caesar cipher, akin to "ROT13". I'll use the ASCII codes for "a" and "A" as "base" ASCII codes. Then for each alphabetic (/[a-zA-Z]/)character to be encoded, I'll use "ord" to get the ASCII code, calculate the "offset" of each letter relative to one of those bases, add the "$int" value, take modulo 26, add the result to the base, and feed the result to "chr" to form the encoded character. (Non-alphabetic characters, I'll send-through verbatim.)
Robbie Hatley's Perl Solution to The Weekly Challenge 358-2
That's it for challenge 358; see you on challenge 359!
Comments
Post a Comment