Posts

Showing posts from December, 2024

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #302 ("Ones and Zeros" and "Step by Step")

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-12-30 through 2025-01-05 is #302. The tasks for challenge #302 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 302-1: "Ones and Zeros" Submitted by: Mohammad Sajid Anwar You are given an array of binary strings, @str, and two integers, $x and $y. Write a script to return the size of the largest subset of @str such that there are at most $x 0’s and $y 1’s in the subset. A set m is a subset of n if all elements of m are also elements of n. Example #1: Input: @str = ("10", "0001", "111001", "1", "0") $x = 5 $y = 3 Output: 4 The largest subset with at most five 0's and three 1's: ("10", "0001", "1", "0") Example #2: Inp...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #301 ("Greatest Number" and "Hamming Distance")

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-12-23 through 2024-12-29 is #301. The tasks for challenge #301 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 301-1: Largest Number Submitted by: Mohammad Sajid Anwar You are given a list of positive integers, @ints. Write a script to arrange all the elements in the given list such that they form the largest number and return it. Example #1: Input: @ints = (20, 3) Output: 320 Example #2: Input: @ints = (3, 30, 34, 5, 9) Output: 9534330 While it's tempting to just "sort and join", while that would work with some numbers (eg, Example #1), it wouldn't work with others (eg, Example #2). So I'll use the non-OOP "permute" function from my favorite CPAN module, "Math::Combinatorics", ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #300 (Beautiful Permutations and Nested Arrays)

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-12-15 through 2024-12-21 is #300. The tasks for challenge #300 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 300-1: Beautiful Arrangements Submitted by: Mohammad Sajid Anwar Description re-written by Robbie Hatley for clarity: Given a positive integer n, write a script to return the number of "beautiful arrangements" within the set of permutations of the sequence (1..n). A 1-indexed permutation of (1..n) is considered "a beautiful arrangement" if for every i (1 <= i <= n) either of the following is true: 1) perm[i] is divisible by i 2) i is divisible by perm[i] Example #1: Input: $n = 2 Output: 2 1st arrangement: [1, 2] perm[1] is divisible by i = 1 perm[2] is divisible by i = 2 2nd arrangement:...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #299 (Word Replacements and Searches)

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-12-08 through 2024-12-14 is #299. The tasks for challenge #299 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 299-1: Replace Words Submitted by: Mohammad Sajid Anwar You are given an array of words and a sentence. Write a script to replace all words in the given sentence that start with any of the words in the given array. Example #1: Input: @words = ("cat", "bat", "rat") $sentence = "the cattle was rattle by the battery" Output: "the cat was rat by the bat" Example #2: Input: @words = ("a", "b", "c") $sentence = "aab aac and cac bab" Output: "a a a c b" Example #3: Input: @words = ("man", "bike") $sen...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #298 ("Maximal Squares" and "Right Intervals")

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-12-01 through 2024-12-07 is #298. The tasks for challenge #298 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 298-1: Maximal Square Submitted by: Mohammad Sajid Anwar You are given an m x n binary matrix with 0 and 1 only. Write a script to find the largest square containing only 1's and return it’s area. Example #1: Input: @matrix = [1, 0, 1, 0, 0] [1, 0, 1, 1, 1] [1, 1, 1, 1, 1] [1, 0, 0, 1, 0] Output: 4 Two maximal square found with same size marked as 'x': [1, 0, 1, 0, 0] [1, 0, x, x, 1] [1, 1, x, x, 1] [1, 0, 0, 1, 0] [1, 0, 1, 0, 0] [1, 0, 1, x, x] [1, 1, 1, x, x] [1, 0, 0, 1, 0] Example #2: Input: @matrix = [0, 1] [1, 0] Output: 1 Two maximal square found with same size marked as 'x': [0, x] [...