Posts

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

Robbie Hatley's Solutions To The Weekly Challenge #245

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-11-26 through 2023-12-02) is weekly challenge #245. Its tasks are as follows: Task 245-1: Sort Language Submitted by: Mohammad S Anwar You are given two arrays: one of languages and the other of their popularities. Write a script to sort the languages based on their popularities. Example 1: Input: @lang = ('perl', 'c', 'python'); @popularity = (2, 1, 3); Output: ('c', 'perl', 'python') Example 2: Input: @lang = ('c++', 'haskell', 'java'); @popularity = (1, 3, 2); Output: ('c++', 'java', 'haskell') I tried solving this problem by "zipping" the two arrays together to make an array of [language, popularity] pairs, then sorting that array numerically by the second elements of the pairs;...

Robbie Hatley's Solutions To The Weekly Challenge #244

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-11-19 through 2023-11-25) is weekly challenge #244: Task 244-1: Count Smaller Submitted by: Mohammad S Anwar You are given an array of integers. Write a script to calculate the number of integers smaller than the integer at each index. Example 1: Input: @int = (8, 1, 2, 2, 3) Output: (4, 0, 1, 1, 3) For index = 0, count of elements less 8 is 4. For index = 1, count of elements less 1 is 0. For index = 2, count of elements less 2 is 1. For index = 3, count of elements less 2 is 1. For index = 4, count of elements less 3 is 3. Example 2: Input: @int = (6, 5, 4, 8) Output: (2, 1, 0, 3) Example 3: Input: @int = (2, 2, 2) Output: (0, 0, 0) Well, the obvious (mundane, prosaic) way is: for each element, riffle through the array and count smaller elements. But let's not do that. Instead, l...

Robbie Hatley's Solutions To The Weekly Challenge #243

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-11-12 through 2023-11-18) is weekly challenge #243. Both of these proved easy to solve by using a pair of three-part loops each: Task 243-1: Reverse Pairs Submitted by: Mohammad S Anwar You are given an array of integers. Write a script to return the number of "reverse pairs" in the given array. A "reverse pair" is a pair (i, j) obeying both of the following: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j]. Example 1: Input: @nums = (1, 3, 2, 3, 1) Output: 2 (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 Example 2: Input: @nums = (2, 4, 3, 5, 1) Output: 3 (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 (3, 4) => nums[3] = 5, nums[4]...

Robbie Hatley's Solutions To The Weekly Challenge #242

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-11-05 through 2023-11-11) is weekly challenge #242. Task 242-1: Missing Members Submitted by: Mohammad S Anwar Given two arrays of integers, write a script to find the [unique] members of each array which are missing in the other array. Example 1: Input: @arr1 = (1, 2, 3); @arr2 = (2, 4, 6); Output: ([1, 3], [4, 6]) (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). Example 2: Input: @arr1 = (1, 2, 3, 3); @arr2 = (1, 1, 2, 2); Output: ([3], []) (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. (1, 1, 2, 2) has 0 members missing in the array (1, 2, 3, 3). The original problem description didn't specify the word [unique], but Example 2 makes it clear that...

Robbie Hatley's Solutions To The Weekly Challenge #241

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-10-29 through 2023-11-04) is weekly challenge #241. Task 241-1 is as follows: PROBLEM DESCRIPTION: Task 1: Arithmetic Triplets Submitted by: Mohammad S Anwar Given an array "nums" of 3-or-more integers in increasing order, and a positive integer "diff", write a script to find the number of unique "Arithmetic Triplets", where an "Arithmetic Triplet" is a trio of numbers from nums which satisfies these rules: a) i < j < k b) nums[j] - nums[i] == diff c) nums[k] - nums[j] == diff Example 1: Input: @nums = (0, 1, 4, 6, 7, 10), $diff = 3 Output: 2 Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. Example 2: Input: @nums = (4, 5, 6, 7...