Posts

Showing posts from December, 2023

Robbie Hatley's Solutions To The Weekly Challenge #249

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 This week (2023-12-24 through 2023-12-30) is weekly challenge #249. Its tasks are as follows: Task 249-1: Equal Pairs Submitted by: Mohammad S Anwar Given an array of integers with even number of elements, write a script to divide the given array into equal pairs such that: a) Each element belongs to exactly one pair. b) The elements present in a pair are equal. Example 1: Input: @ints = (3, 2, 3, 2, 2, 2) Output: (2, 2), (3, 3), (2, 2) There are 6 elements in @ints. They should be divided into 6 / 2 = 3 pairs. @ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the conditions. Example 2: Input: @ints = (1, 2, 3, 4) Output: () There is no way to divide @ints 2 pairs such that the pairs satisfy every condition. To solve this, I made a sub that splices integers from the array and at...

Robbie Hatley's Solutions To The Weekly Challenge #248

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 This week (2023-12-17 through 2023-12-24) is weekly challenge #248. Its tasks are as follows: Task 248-1: Shortest Distance Submitted by: Mohammad S Anwar Rephrased by: Robbie Hatley Given a string and a character in the given string, write a script to return the array of distances abs(i-j) between each index of the string and the index of the nearest copy of the given character within the string, or print an error message if the input is invalid. Example 1: Input: $str = "loveleetcode", $char = "e" Output: (3,2,1,0,1,0,0,1,2,2,1,0) Example 2: Input: $str = "aaab", $char = "b" Output: (3,2,1,0) This can be easily solved by using a pair of nested 3-part loops. The outer loop (over variable i) will look at each index of the string, and the inner loop (over variable...