Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)

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 2025-06-16 through 2025-06-22 is #326

The tasks for challenge #326 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 326-1: Day of the Year
Submitted by: Mohammad Sajid Anwar
You are given a date in the format YYYY-MM-DD. Write a script to
find day number of the year that the given date represent.

Example #1:
Input: $date = '2025-02-02'
Output: 33

Example #2:
Input: $date = '2025-04-10'
Output: 100

Example #3:
Input: $date = '2025-09-07'
Output: 250

To solve this problem, I wrote subs to extract (year, month, day) from strings, check strings for validity, determine whether a given year is a leap year, determine the number of days in a given month in a given year, and return day-of-year. This solution is thus more "structured" than most of my PWCC solutions. Using five subroutines instead of cramming everything into one makes the code easier to read, understand, and maintain.

Robbie Hatley's Perl Solution to The Weekly Challenge 326-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 326-2: Decompressed List
Submitted by: Mohammad Sajid Anwar
You are given an array of positive integers having an even number
of elements. Write a script to to return the decompressed list.
Todecompress, pick adjacent pair (i, j) and replace it with j,
i times.

Example #1:
Input: @ints = (1, 3, 2, 4)
Output: (3, 4, 4)
Pair 1: (1, 3) => 3 one time  => (3)
Pair 2: (2, 4) => 4 two times => (4, 4)

Example #2:
Input: @ints = (1, 1, 2, 2)
Output: (1, 2, 2)
Pair 1: (1, 1) => 1 one time  => (1)
Pair 2: (2, 2) => 2 two times => (2, 2)

Example #3:
Input: @ints = (3, 1, 3, 2)
Output: (1, 1, 1, 2, 2, 2)
Pair 1: (3, 1) => 1 three times => (1, 1, 1)
Pair 2: (3, 2) => 2 three times => (2, 2, 2)

To solve this problem, I wrote two subs: one to check input for validity, and one to "decompress" an array as described in the problem description. For a change, this "task 2" is easier than the corresponding Task 1; I needed 5 subroutines for that one.

Robbie Hatley's Perl Solution to The Weekly Challenge 326-2

That's it for challenge 326; see you on challenge 327!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #319 (“Word Count” and “Minimum Common”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #318 (“Group Positions” and “Reverse Equals”)