Posts

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #398 (“Divisible Average” and “String Difference”)

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-11-02 through 2026-11-08 is #398. The tasks for challenge #398 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 398-1: Divisible Average Submitted by: Mohammad Sajid Anwar You are given an array of positive integers. Write a script to return the closest integer to the mean value of all even integers that are divisible by 3. If none is divisible, return 0. The phrase "even integers that are divisible by 3" is the same as "integers which are divisible by 6", so I need only test "0 == $int % 6", and if there are some of those, return "floor(mean(@ints)+0.5)", else 0. Robbie Hatley's Perl Solution to The Weekly Challenge 398-1 ~~~~~~~~~~~~~~~~~~~~~~...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #397 (“Absent Smallest Positive” and “Binary Reflection”)

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-10-26 through 2026-11-01 is #397. The tasks for challenge #397 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 397-1: Absent Smallest Positive Submitted by: Mohammad Sajid Anwar You are given an array of integers. Write a script to return the smallest absent positive integer such that it is strictly greater than the average of all elements in the given array. I first hash the input array @$aref to %hash, then store it's average in $avg. I then set a variable $asp to the floor of $avg plus 1, or to 1, whichever is greater. I then repeatedly increment $asp for so long as $asp is in %hash. Then I simply return $asp. That yields "the smallest positive integer which is strictly-greater than the average o...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #396 (“Finishing Order” and “Trionic Array”)

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-10-19 through 2026-10-25 is #396. The tasks for challenge #396 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 396-1: Finishing Order Submitted by: Mohammad Sajid Anwar You are given two array of integers, @order and @friends. @order (size n) contains every integer from 1 to n exactly once. @friends contains IDs in sorted in strictly increasing order. Write a script to return the IDs in finishing order. To solve this problem, I use a hash %h with keys being the values of @order and values being the corresponding indexes of @order. I then just sort @friends by {$h{$a}<=>$h{$b}}. Robbie Hatley's Perl Solution to The Weekly Challenge 396-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #016 (“Pythagoras Pie Puzzle” and “Validating Bitcoin Addresses”)

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 tasks for challenge #016 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 016-1: Pythagoras Pie Puzzle Proposed by Jo Christian Oterhals. At a party a pie is to be shared by 100 guests. The first guest gets 1% of the pie, the second guest gets 2% of the remaining pie, the third gets 3% of the remaining pie, the fourth gets 4%, and so on. Write a script that figures out which guest gets the largest piece of pie. I simply performed the calculations described. What happened, I found surprising. Robbie Hatley's Perl Solution to The Weekly Challenge 016-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 016-2: Validating Bitcoin Addresses Write a script to validate a given bitcoin address. Most Bitcoin addr...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #394 (“Alternate Case” and “Alternating Vowels Consonants”)

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-10-05 through 2026-10-11 is #394. The tasks for challenge #394 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 394-1: Alternate Case Submitted by: Mohammad Sajid Anwar You are given a string containing an equal number of uppercase and lowercase English letters. Write a script to the minimum number of adjacent character swaps needed to turn the given string into an alternate case string. We need not perform any swaps at all. Instead, we need only note that there are only two possible ending patterns of uppercase indices: (0, 2, 4,...) or (1, 3, 5,...). We need only count the costs to achieve each of those patterns. And those costs will be the sums of the distances each upper-case letter will have to move to g...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #393 (“Pythagoras Multiplied” and “Prime Step”)

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-09-28 through 2026-10-04 is #393. The tasks for challenge #393 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 393-1: Pythagoras Multiplied Submitted by: Ulrich Rieke You are given a positive integer n. Find the number of all positive integer triplets (a, b, c) so that a^2 + b^2 = c^2 and a, b and c are integers <= n. I'll use a pair of nested ranged foreach loops to check all possible (c,a) pairs to see which (if any) need b values which are positive integers in order for "$a*$a + $b*$b == $c*$c" to be true. Robbie Hatley's Perl Solution to The Weekly Challenge 393-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 393-2: Prime Step Submitted b...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #392 (“Convert Palindrome” and “Words Length Product”)

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-09-21 through 2026-09-27 is #392. The tasks for challenge #392 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 392-1: Convert Palindrome Submitted by: Mohammad Sajid Anwar Write a script to convert a given string to a palindrome by concatenating the minimum number of characters to its left. To solve this problem, I realized that to minimize letters added, I should look for the longest palindromic prefix within the word, then just tack the reversed remainder to the word's left. Robbie Hatley's Perl Solution to The Weekly Challenge 392-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 392-2: Words Length Product Submitted by: Mohammad Sajid Anwar You are give...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #391 (“Array Median” and “Arrange Box”)

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-09-14 through 2026-09-20 is #391. The tasks for challenge #391 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 391-1: Array Median Submitted by: Mohammad Sajid Anwar You are given two sorted arrays of numbers. Write a script to merge the two given sorted arrays and return the median of the merged array. "Merging" is just "my @a3 = sort {$a<=>$b} (@$a1, @$a2);". (Merge Sort won't work, because the problem doesn't state whether the arrays are increasing, decreasing, or going in opposite directions. So I force increasing.) "Median" depends on parity. For an odd number of elements, the median is the middle element. For an even number of elements, the median is one ha...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #390 (“Decode String” and “Order Characters”)

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-09-07 through 2026-09-13 is #390. The tasks for challenge #390 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 390-1: Decode String Submitted by: Mohammad Sajid Anwar You are given an encoded string. Write a script to return the decoded string of the given encoded string. The encoding rule is: K[encoded_string], where the encoded_string inside the square brackets is repeated exactly K > 0 times. I'll use a Perl s/// operator in a while loop to decode innermost bracket pairs for as long as some exist, thus solving the problem from the inside out. Robbie Hatley's Perl Solution to The Weekly Challenge 390-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 39...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #389 (“Reorder Notes” and “ZigZag Subarray”)

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-08-31 through 2026-09-07 is #389. The tasks for challenge #389 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 389-1: Reorder Notes Submitted by: Reinier Maliepaard You are given an array [composer, notes, permutation]. Reconstruct the melody by using each permutation value as the destination position of the corresponding note. Use no explicit for, foreach, or while loops. Output each result as "COMPOSER => reordered notes". ASSUMPTION: Input is valid; the notes array and permutation array have identical lengths, and the permutation contains each position from 1 to N exactly once. I solved this by assigning the original array to an initially-empty array "sliced" by the permutation numb...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #388 (“Dyck Words” and “Secret Santa”)

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-08-24 through 2026-08-30 is #388. The tasks for challenge #388 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 388-1: Dyck Words Submitted by: Mohammad S Anwar A Dyck Word of order $n is a string of length 2x$n consisting of $n ‘U’ (Up) characters and $n ‘D’ (Down) characters such that no initial prefix of the string contains more ‘D’s than ‘U’s. Write a script to return a list of all valid Dyck words of length 2x$n, sorted in lexicographical (alphabetical) order. I first generate likely candidate equivalent integers (with the correct number of binary digits, beginning with 1 and ending with 0). I then check each candidate to see if it actually is a Dyck number. For each Dyck number, I sprintf it as a binar...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #387 (“Rearrange Binary String” and “Atoms Count”)

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-08-17 through 2026-08-23 is #387. The tasks for challenge #387 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 387-1: Rearrange Binary String Submitted by: Mohammad Sajid Anwar You are given a binary string. Write a script to re-arrange the string that all occurrences of “01” are simultaneously replaced with “10” until no occurrences of “01” exist. Return the total steps needed. I'll use a s///g operator in a while loop and count how many times it runs. Robbie Hatley's Perl Solution to The Weekly Challenge 387-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 387-2: Atoms Count Submitted by: Mohammad Sajid Anwar You are given a chemical formula with element...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #386 (“Reverse Base” and “Rational Numbers”)

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-08-10 through 2026-08-16 is #386. The tasks for challenge #386 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 386-1: Reverse Base Submitted by: Mohammad Sajid Anwar You are given a string representing a number, and an integer specifying the base of that representation. Write a function to convert this string to an integer. (For bases greater than 10, use characters A-Z, a-z, + and / in that order.) This is basically a repeat of 384, so I'll just re-use that solution, which uses the base conversion routines in Math::BigInt. Though, I'll have to make some minor tweaks to allow for the collation sequence specified in 386. Robbie Hatley's Perl Solution to The Weekly Challenge 386-1 ...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #385 (“Uncommon Words” and “Outermost Parentheses”)

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-08-03 through 2026-08-09 is #385. The tasks for challenge #385 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 385-1: Uncommon Words Submitted by: Mohammad Sajid Anwar You are given two sentences. Write a script to return a list of all uncommon words. Order is not important. Judging by the answers given to the examples, I see that the word "uncommon" is being used to mean "used once only", not "not in-common between sentences". So I use a hash to keep track of total occurrences of words, and I return only those words which occur exactly once. Robbie Hatley's Perl Solution to The Weekly Challenge 385-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #384 (“Base N” and “Special Binary Substrings”)

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-27 through 2026-08-02 is #384. The tasks for challenge #384 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 384-1: Base N Submitted by: Mohammad Sajid Anwar You are given a number and a base integer. Write a script to convert the given number in the given base integer. To solve this problem, I use the excellent base conversion routines provided by "Math::BigInt", augmented by Gnu's "GMP" library, which will provide both blazing speed and unlimited precision. (I usually "roll my own" in these challenges, but not this time; it would be an insult to the hard-working authors of "Math::BigInt" and "GMP" for me to NOT use their products in a case where they...

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