Posts

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #327 (“Missing Integers” and “Minimum Absolute Difference”)

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 2025-06-23 through 2025-06-29 is #327 The tasks for challenge #327 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 327-1: Missing Integers Submitted by: Mohammad Sajid Anwar You are given an array of n integers. Write a script to find all the missing integers in the range 1..n in the given array. Example 1 Input: @ints = (1, 2, 1, 3, 2, 5) Output: (4, 6) Example 2 Input: @ints = (1, 1, 1) Output: (2, 3) Example 3 Input: @ints = (2, 2, 1) Output: (3) To solve this, I use function "none" in CPAN module "List::Util" to determine which numbers of 1..n are equal to none of the elements of the array, then I return those numbers. Robbie Hatley's Perl Solution to The Weekly Ch...

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

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

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #325 (“Consecutive Ones” and “Final Price”)

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

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #324 (“2D Array” and “Total XOR”)

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 2025-06-02 through 2025-06-08 is #324 The tasks for challenge #324 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 324-1: 2D Array Submitted by: Mohammad Sajid Anwar You are given an array of integers and two integers $r amd $c. Write a script to create two dimension array having $r rows and $c columns using the given array. Example #1: Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2 Output: ([1, 2], [3, 4]) Example #2: Input: @ints = (1, 2, 3), $r = 1, $c = 3 Output: ([1, 2, 3]) Example #3: Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1 Output: ([1], [2], [3], [4]) To reshape a 1D array into a 2D array using same contents, one should really use APL, in which case the entire program would only be a few characters...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #323 (“Increment Decrement” and “Tax Amount”)

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 2025-05-26 through 2025-06-01 is #323 The tasks for challenge #323 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 323-1: Increment Decrement Submitted by: Mohammad Sajid Anwar You are given a list of operations. Write a script to return the final value after performing the given operations in order. The initial value is always 0. Possible Operations: ++x or x++: increment by 1 --x or x--: decrement by 1 Example #1: Input: @operations = ("--x", "x++", "x++") Output: 1 Operation "--x" => 0 - 1 => -1 Operation "x++" => -1 + 1 => 0 Operation "x++" => 0 + 1 => 1 Example #2: Input: @operations = ("x++", "++x", ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #322 (“String Format” and “Rank Array”)

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 2025-05-19 through 2025-05-25 is #322 The tasks for challenge #322 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 322-1: String Format Submitted by: Mohammad Sajid Anwar You are given a string and a positive integer. Write a script to format the string, removing any dashes, in groups of size given by the integer. The first group can be smaller than the integer but should have at least one character. Groups should be separated by dashes. Example #1: Input: $str = "ABC-D-E-F", $i = 3 Output: "ABC-DEF" Example #2: Input: $str = "A-BC-D-E", $i = 2 Output: "A-BC-DE" Example #3: Input: $str = "-A-B-CD-E", $i = 4 Output: "A-BCDE" I first strip-out all t...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #321 (“Distinct Average” and “Backspace Compare”)

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of taks each Monday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-05-12 through 2025-05-18 is #321 The tasks for challenge #321 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 321-1: Distinct Average Submitted by: Mohammad Sajid Anwar You are given an array of numbers with even length. Write a script to return a count of distinct averages. The averages are calculated by removing the minimum and the maximum, then averaging the two. Example #1: Input: @nums = (1, 2, 4, 3, 5, 6) Output: 1 Step 1: Min = 1, Max = 6, Avg = 3.5 Step 2: Min = 2, Max = 5, Avg = 3.5 Step 3: Min = 3, Max = 4, Avg = 3.5 The count of distinct averages is 1. Example #2: Input: @nums = (0, 2, 4, 8, 3, 5) Output: 2 Step 1: Min = 0, Max = 8, Avg = 4 Step 2: Min = 2, Max = 5, Avg = 3.5 Step 3: Min = 3, Max...