Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #325 (“Consecutive Ones” and “Final Price”)
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 for the week of 2025-06-09 through 2025-06-15 is #325
The tasks for challenge #325 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 325-1: Consecutive Ones Submitted by: Mohammad Sajid Anwar You are given a binary array containing only 0s or/and 1s. Write a script to find the maximum consecutive 1s in the given array. Example #1: Input: @binary = (0, 1, 1, 0, 1, 1, 1) Output: 3 Example #2: Input: @binary = (0, 0, 0, 0) Output: 0 Example #3: Input: @binary = (1, 0, 1, 0, 1, 1) Output: 2
To solve this problem, I'll make a sub that counts each cluster of 1s and keeps track of the max count seen.

Robbie Hatley's Perl Solution to The Weekly Challenge 325-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 325-2: Final Price Submitted by: Mohammad Sajid Anwar You are given an array of item prices. Write a script to find the final price of each items in the given array. There is a special discount scheme going on. If there’s an item with a lower or equal price later in the list, you get a discount equal to that later price (the first one you find in order). Example inputs: [8, 4, 6, 2, 3], [1, 2, 3, 4, 5], [7, 1, 1, 5] Expected outputs: (4, 2, 4, 2, 3), (1, 2, 3, 4, 5), (6, 0, 1, 5)
Nested 3-part loops on array indices will allow us to easily see if there is a "first item with greater index but less-than-or-equal price" present, and if so, apply that price as "discount" to current price.

Robbie Hatley's Perl Solution to The Weekly Challenge 325-2
That's it for challenge 325; see you on challenge 326!
Comments
Post a Comment