Posts

Robbie Hatley's Solutions To The Weekly Challenge #268

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-05-05 through 2024-05-11 is #268. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 268-1: Magic Number Submitted by: Mohammad Sajid Anwar You are given two arrays of integers of same size, @x and @y. Write a script to find the magic number which when added to each element of the first array gives the second array. Element order is not important. Example 1: Input: @x = (3, 7, 5) @y = (9, 5, 7) Output: 2 The magic number is 2. @x = (3, 7, 5) + 2 2 2 @y = (5, 9, 7) Example 2: Input: @x = (1, 2, 1) @y = (5, 4, 4) Output: 3 The magic number is 3. @x = (1, 2, 1) + 3 3 3 @y = (5, 4, 4) Example 3: Input: @x = (2) @y = (5) Output: 3 I'll sort both arrays then subtract the second from t

Robbie Hatley's Solutions To The Weekly Challenge #267

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 (2024-04-28 through 2024-05-04) is weekly challenge #267. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 267-1: Product Sign Submitted by: Mohammad Sajid Anwar You are given an array of @ints. Write a script to find the sign of the product of all integers in the given array. The sign is 1 if the product is positive, -1 if the product is negative, and 0 if product is zero. Example 1 input: [-1, -2, -3, -4, 3, 2, 1] Expected output: 1 Example 2 input: [1, 2, 0, -2, -1] Expected output: 0 Example 3 input: [-1, -1, 1, -1, 2] Expected output: -1 The sign of the product is the product of the signs, and sign(x) is given by "0 if x is 0, else x/abs(x)". So these subs should work: use v5.36; use List::Util 'product'; su

Robbie Hatley's Solutions To The Weekly Challenge #266

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 (2024-04-21 through 2024-04-27) is weekly challenge #266. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 266-1: Uncommon Words Submitted by: Mohammad Sajid Anwar You are given two sentences, $line1 and $line2. Write a script to find all "uncommmon" words in any order in the given two sentences, or return ('') if none are found. A word is "uncommon" if it appears exactly once in one of the sentences and doesn’t appear in other sentence. Example 1: Input: $line1 = 'Mango is sweet' $line2 = 'Mango is sour' Output: ('sweet', 'sour') Example 2: Input: $line1 = 'Mango Mango' $line2 = 'Orange' Output: ('Orange') Example 3: Input: $line1 = 'Mango is Ma

Robbie Hatley's Solutions To The Weekly Challenge #265

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 (2024-04-14 through 2024-04-20) is weekly challenge #265. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 265-1: 33% Appearance Submitted by: Mohammad Sajid Anwar You are given an array of integers, @ints. Write a script to find an integer in the given array that appeared 33% or more. If more than one found, return the smallest. If none found, then return undef. Example 1: Input: @ints = (1,2,3,3,3,3,4,2) Output: 3 1 appeared 1 times. 2 appeared 2 times. 3 appeared 4 times. 3 appeared 50% (>33%) in the given array. Example 2: Input: @ints = (1,1) Output: 1 1 appeared 2 times. 1 appeared 100% (>33%) in the given array. Example 3: Input: @ints = (1,2,3) Output: 1 1 appeared 1 times. 2 appeared 1 times. 3 appeared 1 times. Since all three app

Robbie Hatley's Solutions To The Weekly Challenge #264

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 (2024-04-07 through 2024-04-13) is weekly challenge #264. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 264-1: Greatest English Letter Submitted by: Mohammad Sajid Anwar You are given a string, $str, made up of only alphabetic characters [a..zA..Z]. Write a script to return the greatest english letter in the given string. (A letter is "greatest" if it occurs as lower and upper case. Also letter ‘b’ is greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet. Example 1: Input: $str = 'PeRlwEeKLy' Output: L There are two letters E and L that appears as lower and upper. The letter L appears after E, so the L is the greatest english letter. Example 2: Input: $str = 'ChaLlenge' Output: L Example 3: Input: $str = &

Robbie Hatley's Solutions To The Weekly Challenge #263

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 (2024-03-31 through 2024-04-06) is weekly challenge #263. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 263-1: Target Index Submitted by: Mohammad Sajid Anwar You are given an array of integers @ints and a target element $k. Write a script to return the list of indices in the sorted array where the element is same as the given target element. Example 1: Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 Output: (1, 2) Sorted array: (1, 2, 2, 3, 4, 5) Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 Example 2: Input: @ints = (1, 2, 4, 3, 5), $k = 6 Output: () No element in the given array matching the given target. Example 3: Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 Output: (4) Sorted array: (1, 2, 2, 3, 4, 5) Target index: (4) as $ints[4] = 4

Robbie Hatley's Solutions To The Weekly Challenge #262

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 (2024-03-247 through 2024-03-30) is weekly challenge #262. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 262-1: Max Positive Negative Submitted by: Mohammad Sajid Anwar You are given an array of integers, @ints. Write a script to return the maximum number of either positive or negative integers in the given array. Example 1: Input: @ints = (-3, 1, 2, -1, 3, -2, 4) Output: 4 Count of positive integers: 4 Count of negative integers: 3 Maximum of count of positive and negative integers: 4 Example 2: Input: @ints = (-1, -2, -3, 1) Output: 3 Count of positive integers: 1 Count of negative integers: 3 Maximum of count of positive and negative integers: 3 Example 3: Input: @ints = (1,2) Output: 2 Count of positive integers: 2 Count of negative intege