Posts

Showing posts from November, 2023

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