Posts

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