Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #387 (“Rearrange Binary String” and “Atoms Count”)
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 elements, numbers, and parentheses. Write a script to count the total number of each type of atom by expanding all grouped multipliers. Then, format and return the final inventory as a single string sorted alphabetically by element name, including the total count only if it is greater than 1.
I'll use one subroutine to expand inner blocks, and another to count element abundances using a hash.
Robbie Hatley's Perl Solution to The Weekly Challenge 387-2
That's it for challenge 387; see you on challenge 388!
Comments
Post a Comment