Posts

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

Robbie Hatley's Solutions To The Weekly Challenge #240

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-22 through 2023-10-28) is weekly challenge #240. Task 240-1 is as follows: Task 1: Acronym Submitted by: Mohammad S Anwar You are given an array of strings and a check string. Write a script to find out if the check string is the acronym of the words in the given array. Example 1: Input: @str = ("Perl", "Python", "Pascal"); $chk = "ppp"; Output: true Example 2: Input: @str = ("Perl", "Raku"); $chk = "rp"; Output: false Example 3: Input: @str = ("Oracle", "Awk", "C"); $chk = "oac"; Output: true This is just a matter of making these two subs: sub acronym (@list) {return join('', map {substr $_, 0, 1} @list)} sub ncscomp ($x,$y) {return (fc($x) eq fc($y))}

Robbie Hatley's Solutions To The Weekly Challenge #239

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-15 through 2023-10-21) is weekly challenge #239. Task 239-1 is as follows: Task 1: Same String Submitted by: Mohammad S Anwar Given two arrays of strings, write a script to find out if the word created by concatenating the array elements is the same. Example 1: Input: @arr1 = ("ab", "c") @arr2 = ("a", "bc") Output: true Using @arr1, word1 => "ab" . "c" => "abc" Using @arr2, word2 => "a" . "bc" => "abc" Example 2: Input: @arr1 = ("ab", "c") @arr2 = ("ac", "b") Output: false Using @arr1, word1 => "ab" . "c" => "abc" Using @arr2, word2 => "ac" . "b" => "acb&