Posts

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #383 (“Similar List” and “Nearest RGB”)

Image
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-07-20 through 2026-07-26 is #383. If anyone is wondering what happened to 377 through 382, I've been quite busy the last two months adjusting to my life as a "newly-retired person", so I haven't had the attention for The Perl Weekly Challenge Club. But after a 42-day absence, I'm back! I may eventually solve the skipped challenges and post my results here, but for now, here is Challenge 383. The tasks for challenge #383 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 383-1: Similar List Submitted by: Mohammad Sajid Anwar You are given two lists of strings and a list of "similar-words groups". Write a script to find out if the two word lists are "similar" with the help ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #376 (“Chessboard Squares” and “Doubled Words”)

Image
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-06-01 through 2026-06-07 is #376. The tasks for challenge #376 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 376-1: Chessboard Squares Submitted by: Mohammad Sajid Anwar Write a script to determine if two chess-board squares (specified by standard chess notation) have the same color. To solve this problem, I query whether the sum of the first coordinate pair and the sum of the second coordinate pair have the same modulo 2. Robbie Hatley's Perl Solution to The Weekly Challenge 376-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 376-2: Doubled Words Submitted by: Matt Martini You are given a string (which may contain embedded newlines) which is taken from a pag...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #375 (“Single Common Word” and “Find K-Beauty”)

Image
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-05-25 through 2026-05-31 is #375. The tasks for challenge #375 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 375-1: Single Common Word Submitted by: Mohammad Sajid Anwar You are given two arrays of strings. Write a script to return the number of strings that appear exactly once in each of the two given arrays. String comparison is case sensitive. This looks like a job for "scalar grep {$_ eq $s} @a". Robbie Hatley's Perl Solution to The Weekly Challenge 375-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 375-2: Find K-Beauty Submitted by: Mohammad Sajid Anwar You are given a positive integer and a digit K. Write a script to find the K-Beauty of the ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #373 (“Equal List” and “List Division”)

Image
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-05-11 through 2026-05-17 is #373. The tasks for challenge #373 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 373-1: Equal List Submitted by: Mohammad Sajid Anwar You are given two arrays of strings. Write a script to return true if the two given array yield the same strings when joined; otherwise return false. I've reworded the above "description" to be much more unambiguous than the version on the web site. This is just a matter of joining and comparing. Robbie Hatley's Perl Solution to The Weekly Challenge 373-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 373-2: List Division Submitted by: Mark Anderson You are given a list and a non-negativ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #372 (“Rearrange Spaces” and “Largest Substring”)

Image
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-05-04 through 2026-05-10 is #372. The tasks for challenge #372 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 372-1: Rearrange Spaces Submitted by: Mohammad Sajid Anwar You are given a string text of words that are placed among a number of spaces. Write a script to rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximised. If you can’t distribute, place the extra spaces at the end. Finally return the string. I use this procedure: Count available spaces s. Trim leading and trailing spaces. Split string to @words on spaces. Count gaps between words (n words means n-1 gaps). Form quotient q and remainder r of sp...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #371 (“Missing Letter” and “Subset Equilibrium”)

Image
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-04-27 through 2026-05-03 is #371. The tasks for challenge #371 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 371-1: Missing Letter Submitted by: Reinier Maliepaard You are given a sequence of 5 lowercase letters, with one letter replaced by ‘?’. Each letter maps to its position in the alphabet (‘a = 1’, ‘b = 2’, …, ‘z = 26’). The sequence follows a repeating pattern of step sizes between consecutive letters. The pattern is either a constant step (e.g., ‘+2, +2, +2, +2’) or a simple alternating pattern of two distinct steps (e.g., ‘+2, +3, +2, +3’). Example inputs: ("ac?gi", "ad?jm", "ae?mq", "acf?k", "beg?l") Expected outputs: e g i ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #370 (“Popular Word” and “Scramble String”)

Image
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-04-20 through 2026-04-26 is #370. The tasks for challenge #370 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 370-1: Popular Word Submitted by: Mohammad Sajid Anwar You are given a string paragraph and an array of banned words. Write a script to return the most popular word that is not banned. It is guaranteed there is at least one word that is not banned and the answer is unique. The words in paragraph are case-insensitive and the answer should be in lowercase. The words cannot contain punctuation symbols. Example #1: Inputs: [ "Bob hit a ball, the hit BALL flew far after it was hit.", ["hit"], ], Output: "ball" After removing punctuation and converting to lower...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #369 (“Valid Tag” and “Group Division”)

Image
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-04-13 through 2026-04-19 is #369. The tasks for challenge #369 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 369-1: Valid Tag Submitted by: Mohammad Sajid Anwar You are given a given a string caption for a video. Write a script to generate tag for the given string caption in three steps as mentioned below: 1. Format as camelCase Starting with a lower-case letter and capitalising the first letter of each subsequent word. Merge all words in the caption into a single string starting with a #. 2. Sanitise the String Strip out all characters that are not English letters (a-z or A-Z). 3. Enforce Length If the resulting string exceeds 100 characters, truncate it so it is exactly 100 characters long. Example #1: I...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #368 (“Make it Bigger” and “Big and Little Omega”)

Image
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-04-06 through 2026-04-12 is #368. The tasks for challenge #368 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 368-1: Make it Bigger Submitted by: Mohammad Sajid Anwar You are given a given a string number and a character digit. Write a script to remove exactly one occurrence of the given character digit from the given string number, resulting in the number being maximised. Example #1: Input: $str = "15456", $char = "5" Output: "1546" Removing the second "5" is better because the digit following it (6) is greater than 5. In the first case, 5 was followed by 4 (a decrease), which makes the resulting number smaller. Example #2: Input: $str = "7332", $char = ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #367 (“Max Odd Binary” and “Conflict Events”)

Image
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-30 through 2026-04-05 is #367. The tasks for challenge #367 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 367-1: Max Odd Binary Submitted by: Mohammad Sajid Anwar You are given a binary string that has at least one ‘1’. Write a script to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number and return the resulting binary string. The resulting string can have leading zeros. Example 1 Input: $str = "1011" Output: "1101" Example 2 Input: $str = "100" Output: "001" Example 3 Input: $str = "111000" Output: "110001" Example 4 Input: $str = "0101" Output: "1001" Example 5 Input: $str =...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #366 (“Count Prefixes” and “Valid Times”)

Image
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-23 through 2026-03-29 is #366. The tasks for challenge #366 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 366-1: Count Prefixes Submitted by: Mohammad Sajid Anwar You are given an array of words and a string (contains only lowercase English letters). Write a script to return the number of words in the given array that are a prefix of the given string. ( # Example #1 input: [["a", "ap", "app", "apple", "banana"], "apple"], # Expected output: 4 # Example #2 input: [["cat", "dog", "fish"], "bird"], # Expected output: 0 # Example #3 input: [["hello", "he", "hell...

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

Image
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...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #364 (“Decrypt String” and “Goal Parser”)

Image
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-09 through 2026-03-15 is #364. The tasks for challenge #364 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 364-1: Decrypt String Submitted by: Mohammad Sajid Anwar You are given a string formed by digits and ‘#'. Write a script to map the given string to English lowercase characters given the following two rules: 1: Characters 'j' to 'z' are represented by '10#' to '26#'. 2: Characters 'a' to 'i' are represented by '1' to '9'. Example #1: Input: $str = "10#11#12" Output: "jkab" Example #2: Input: $str = "1326#" Output: "acz" Example #3: Input: $str = "25#24#123" Output: "yxabc" ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #363 (“String Lie Detector” and “Subnet Sheriff”)

Image
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-02 through 2026-03-08 is #363. The tasks for challenge #363 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 363-1: String Lie Detector Submitted by: Mohammad Sajid Anwar You are given a string. Write a script that parses a self-referential string and determines whether its claims about itself are true. The string will make statements about its own composition, specifically the number of vowels and consonants it contains. ( # Example #1 input: "aa — two vowels and zero consonants", # Expected output: true # Example #2 input: "iv — one vowel and one consonant", # Expected output: true # Example #3 input: "hello - three vowels and two consonants", # Exp...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #362 (“Echo Chamber” and “Spellbound Sorting”)

Image
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-02-23 through 2026-03-01 is #362. The tasks for challenge #362 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 362-1: Echo Chamber Submitted by: Mohammad Sajid Anwar You are given a string containing lowercase letters. Write a script to transform the string based on the index position of each character (starting from 0). For each character at position i, repeat it i + 1 times. Example #1: Input: "abca" Output: "abbcccaaaa" Example #2: Input: "xyz" Output: "xyyzzz" Example #3: Input: "code" Output: "coodddeeee" Example #4: Input: "hello" Output: "heelllllllooooo" Example #5: Input: "a" Output: "a" This is ju...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #361 (“Zeckendorf Representation” and “Find Celebrity”)

Image
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-02-16 through 2026-02-22 is #361. The tasks for challenge #361 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 361-1: Zeckendorf Representation Submitted by: Mohammad Sajid Anwar You are given a positive integer (<= 100). Write a script to return Zeckendorf Representation of the given integer. Every positive integer can be uniquely represented as sum of non-consecutive Fibonacci numbers. Example 1 Input: $int = 4 Output: 3,1 4 => 3 + 1 (non-consecutive Fibonacci numbers) Example 2 Input: $int = 12 Output: 8,3,1 12 => 8 + 3 + 1 Example 3 Input: $int = 20 Output: 13,5,2 20 => 13 + 5 + 2 Example 4 Input: $int = 96 Output: 89,5,2 96 => 89 + 5 + 2 Example 5 Input: $int = 100 Output: 89,8,3 100 =...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #360 (“Text Justifier” and “Word Sorter”)

Image
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-02-09 through 2026-02-15 is #360. The tasks for challenge #360 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 360-1: Text Justifier Submitted by: Mohammad Sajid Anwar You are given a string and a width. Write a script to return the string that centers the text within that width using asterisks * as padding. ( # Example #1 input: ["Hi", 5], # Expected output: "*Hi**" # Example #2 input: ["Code", 10], # Expected output: "***Code***" # Example #3 input: ["Hello", 9], # Expected output: "**Hello**" # Example #4 input: ["Perl", 4], # Expected output: "Perl" # Example #5 input: ["A"...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #359 (“Digital Root” and “String Reduction”)

Image
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-02-02 through 2026-02-08 is #359. The tasks for challenge #359 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 359-1: Digital Root Submitted by: Mohammad Sajid Anwar You are given a positive integer, $int. Write a function that calculates the additive persistence of a positive integer and also return the digital root. Digital root is the recursive sum of all digits in a number until a single digit is obtained. Additive persistence is the number of times you need to sum the digits to reach a single digit. Example #1: Input: $int = 38 Expected output: Digital Root = 2; Persistence = 2 Example #2: Input: $int = 7 Expected output: Digital Root = 7; Persistence = 0 Example #3: Input: $int = 999 Expected output: Di...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #358 (“Max Str Value” and “Encrypted String”)

Image
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 ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #357 (“Kaprekar Constant” and “Unique Fraction Generator”)

Image
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-19 through 2026-01-25 is #357. The tasks for challenge #357 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 357-1: Kaprekar Constant Submitted by: Mohammad Sajid Anwar Write a function that takes a 4-digit integer and returns how many iterations of the following algorithm are required to reach Kaprekar’s constant (6174). (For more information about Kaprekar's Constant, see "https://en.wikipedia.org/wiki/6174".) 1. Select any four-digit number that has at least two different digits (leading zeros are allowed). 2. Create two new four-digit numbers by arranging the original digits in a.) ascending and b.) descending order (adding leading zeros if necessary). 3. Subtract...