Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #397 (“Absent Smallest Positive” and “Binary Reflection”)
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 of the array and which is absent from the array", as the problem requires.
Robbie Hatley's Perl Solution to The Weekly Challenge 397-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 397-2: Binary Reflection Submitted by: Mohammad Sajid Anwar You are given an array of integers. Write a script to sort the given array in ascending order based on the binary reflection of each element. If two different numbers have the same binary reflection, the smaller original number should appear first.
I use two subs: "br" (which returns the binary reflection of an integer), and "reflection_sort" (which sorts integers firstly by binary reflection, and secondly by value).
Also note that the problem description is flawed, because "binary reflection" is not uniquely defined for negative integers, because the number of "1" bits used depends on the size (in bits) of integer type being used, so it would be different for 8-bit, 16-bit, 32-bit, and 64-bit integers. And Perl doesn't make it easy to determine what it's using under-the-hood to represent negative integers. Therefore I restrict this program to non-negative integers only.
Robbie Hatley's Perl Solution to The Weekly Challenge 397-2
That's it for challenge 397; see you on challenge 398!
Comments
Post a Comment