Posts

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #309 ("Min Gap" and "Min Diff")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-02-17 through 2025-02-23 is #309. The tasks for challenge #309 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 309-1: Min Gap Submitted by: Mohammad Sajid Anwar You are given an array of integers, @ints, increasing order. Write a script to return the element before which you find the smallest gap. Example #1: Input: @ints = (2, 8, 10, 11, 15) Output: 11 8 - 2 => 6 10 - 8 => 2 11 - 10 => 1 15 - 11 => 4 11 is where we found the min gap. Example #2: Input: @ints = (1, 5, 6, 7, 14) Output: 6 5 - 1 => 4 6 - 5 => 1 7 - 6 => 1 14 - 7 => 7 6 and 7 where we found the min gap, so we pick the first instance. Example #3: Input: @ints = (8, 20, 25, 28) Output: 28 8 - 20 => 14 25 - 20 => 5 28 - 25 ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #308 ("Count Common" and "Decode XOR")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-02-10 through 2025-02-16 is #308. The tasks for challenge #308 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 308-1: Count Common Submitted by: Mohammad Sajid Anwar You are given two array of strings, @str1 and @str2. Write a script to return the count of common strings in both arrays. Example #1: Input: @str1 = ("perl", "weekly", "challenge") @str2 = ("raku", "weekly", "challenge") Output: 2 Example #2: Input: @str1 = ("perl", "raku", "python") @str2 = ("python", "java") Output: 1 Example #3: Input: @str1 = ("guest", "contribution") @str2 = ("fun", "weekly...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #307 ("Check Order" and "Find Anagrams")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-02-03 through 2025-02-09 is #307. The tasks for challenge #307 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 307-1: "Check Order" Submitted by: Mohammad Sajid Anwar You are given an array of integers, @ints. Write a script to re-arrange the given array in an increasing order and return the indices where it differs from the original array. Example #1: Input: @ints = (5, 2, 4, 3, 1) Output: (0, 2, 3, 4) Before: (5, 2, 4, 3, 1) After : (1, 2, 3, 4, 5) Difference at indices: (0, 2, 3, 4) Example #2: Input: @ints = (1, 2, 1, 1, 3) Output: (1, 3) Before: (1, 2, 1, 1, 3) After : (1, 1, 1, 2, 3) Difference at indices: (1, 3) Example #3: Input: @ints = (3, 1, 3, 2, 3) Output: (0, 1, 3) Before: (3, 1, 3, 2, 3) After : ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #306 ("Odd Sum" and "Last Element")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2025-01-27 through 2025-02-02 is #306. The tasks for challenge #306 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 306-1: "Odd Sum" Submitted by: Mohammad Sajid Anwar You are given an array of positive integers, @ints. Write a script to return the sum of all possible odd-length subarrays of the given array. A subarray is a contiguous subsequence of the array. Example #1: Input: @ints = (2, 5, 3, 6, 4) Output: 77 Odd length sub-arrays: (2) => 2 (5) => 5 (3) => 3 (6) => 6 (4) => 4 (2, 5, 3) => 10 (5, 3, 6) => 14 (3, 6, 4) => 13 (2, 5, 3, 6, 4) => 20 Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77 Example #2: Input: @ints = (1, 3) Output: 4 I'll solve this problem by using nest...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #305 ("Binary Prefix" and "Alien Dictionary")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2024-01-20 through 2025-01-26 is #305. The tasks for challenge #305 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 305-1: "Binary Prefix" Submitted by: Mohammad Sajid Anwar You are given a binary array. Write a script to return an array of booleans where the partial binary number up to that point is prime. Example #1: Input: @binary = (1, 0, 1) Output: (false, true, true) Sub-arrays (base-10): (1): 1 - not prime (1, 0): 2 - prime (1, 0, 1): 5 - prime Example #2: Input: @binary = (1, 1, 0) Output: (false, true, false) Sub-arrays (base-10): (1): 1 - not prime (1, 1): 3 - prime (1, 1, 0): 6 - not prime Example #3: Input: @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1) Output: (false, true, true...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #304 ("Arrange Binary" and "Maximum Average")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2024-01-13 through 2025-01-19 is #304. The tasks for challenge #304 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 304-1: "Arrange Binary" Submitted by: Mohammad Sajid Anwar Reworded for clarity by Robbie Hatley. You are given a list @d of 0s and 1s and a positive integer $n. Write a script to return true if you can re-arrange the list by replacing at least $n "0" digits of @d with "1" in such a way that no two consecutive digits are 1, otherwise return false. Example #1: Input: @digits = (1, 0, 0, 0, 1), $n = 1 Output: true Re-arranged list: (1, 0, 1, 0, 1) Example #2: Input: @digits = (1, 0, 0, 0, 1), $n = 2 Output: false First of all, @d needs to be checked to see if it already has any conse...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #303 ("3-Digits Even" and "Delete and Earn")

Image
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2024-01-06 through 2025-01-12 is #303. The tasks for challenge #303 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 303-1: "3-Digits Even" Submitted by: Mohammad Sajid Anwar You are given a list (3 or more) of positive integers, @ints. Write a script to return all even 3-digits integers that can be formed using the integers in the given list. Example #1: Input: @ints = (2, 1, 3, 0) Output: (102, 120, 130, 132, 210, 230, 302, 310, 312, 320) Example #2: Input: @ints = (2, 2, 8, 8, 2) Output: (222, 228, 282, 288, 822, 828, 882) This looks like yet another job for my favorite CPAN module, "Math::Combinatorics". I'll first extract the digits from @ints and push them to an array "@digits", then I...