Posts

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 Other than the hassle of having to generate all of the permutations of (1..n), then counting how many of them are "beautiful arrangements", this is conceptually straightforward, if a bit complex (O(n!)). One snag is the requirement f...

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

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #297 ("Contiguous Sub-Arrays" and "Semi-Ordered Permutations")

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-11-24 through 2024-11-30 is #297. The tasks for challenge #297 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 297-1: Contiguous Sub-Arrays Submitted by: Mohammad Sajid Anwar Write a script which, given an array of 1-digit-binary integers (0s and 1s), determines the maximum length of contiguous sub-arrays with equal numbers of 0s and 1s. Example #1: Input: @binary = (1, 0) Output: 2 (1, 0) is the longest contiguous subarray with an equal number of 0 and 1. Example #2: Input: @binary = (0, 1, 0) Output: 2 (1, 0) or (0, 1) is the longest contiguous subarray with an equal number of 0 and 1. Example #3: Input: @binary = (0, 0, 0, 0, 0) Output: 0 Example #4: Input: @binary = (0, 1, 0, 0, 1, 0) Output: 4 (1,0,0,1) I'll ...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #296 ("String Compression & Matchstick Squares")

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-11-17 through 2024-11-23 is #296. The tasks for challenge #296 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 296-1: String Compression Submitted by: Mohammad Sajid Anwar You are given a string of alphabetic characters, $chars. Write a script to compress the string with run-length encoding, as shown in the examples. A compressed unit can be either a single character or a count followed by a character. BONUS: Write a decompression function. Example 1: Input: $chars = "abbc" Output: "a2bc" Example 2: Input: $chars = "aaabccc" Output: "3ab3c" Example 3: Input: $chars = "abcc" Output: "ab2c" Since this problem involves an input consisting of a string of character...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #295 ("Recursive Segmentation")

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-11-10 through 2024-11-16 is #295. The tasks for challenge #295 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 295-1: Word Break Submitted by: Mohammad Sajid Anwar Write a script to return true-or-false depending on whether- or-not a given string can be segmented into a sequence of one-or-more words from a given list of words. Example 1: Input: string = 'weeklychallenge' words = ("challenge", "weekly") Output: true Example 2 Input: string = 'perlrakuperl' words = ("raku", "perl") Output: true Example 3 Input: string = 'sonsanddaughters' words = ("sons", "sand", "daughters") Output: false This problem (like the sec...

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #294 ("Consecutive Sequence" and "Next Permutation")

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-11-03 through 2024-11-09 is #294. The tasks for challenge #294 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 294-1: Consecutive Sequence Submitted by: Mohammad Sajid Anwar You are given an unsorted array of integers, @ints. Write a script to return the length of the longest consecutive elements sequence. Return -1 if none found. The algorithm must run in O(n) time. Example 1: Input: @ints = (10, 4, 20, 1, 3, 2) Output: 4 The longest consecutive sequence (1, 2, 3, 4). The length of the sequence is 4. Example 2: Input: @ints = (0, 6, 1, 8, 5, 2, 4, 3, 0, 7) Output: 9 Example 3: Input: @ints = (10, 30, 20) Output: -1 I could first sort the array, but that would be O(n*log(n)), not O(n). Faster will be to make a hash and...