Posts

Robbie Hatley's Solutions To The Weekly Challenge #273

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-06-09 through 2024-06-15 is #273. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 273-1: Percentage of Character Submitted by: Mohammad Sajid Anwar You are given a string, $str and a character $chr. Write a script to return the nearest integer percentage of the characters in $str which are $chr. Example 1: Input: $str = "perl", $chr = "e" Output: 25 Example 2: Input: $str = "java", $chr = "a" Output: 50 Example 3: Input: $str = "python", $chr = "m" Output: 0 Example 4: Input: $str = "ada", $chr = "a" Output: 67 Example 5: Input: $str = "ballerina", $chr = "l" Output: 22 Example 6: Input: $str = "analitik&quo

Robbie Hatley's Solutions To The Weekly Challenge #272

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-06-02 through 2024-06-08 is #272. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 272-1: Defang IP Address Submitted by: Mohammad Sajid Anwar You are given a valid IPv4 address. Write a script to return the defanged version of the given IP address. A "defanged" IP address replaces every period “.” with “[.]". Example 1: Input: $ip = "1.1.1.1" Output: "1[.]1[.]1[.]1" Example 2: Input: $ip = "255.101.1.0" Output: "255[.]101[.]1[.]0" The "s" operator makes quick work of this: sub defang ($x) {$x =~ s/\./[.]/gr} Robbie Hatley's Perl Solution to The Weekly Challenge 272-1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Robbie Hatley's Solutions To The Weekly Challenge #271

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-26 through 2024-06-01 is #271. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 271-1: Maximum Ones Submitted by: Mohammad Sajid Anwar You are given a m x n binary matrix. Write a script to return the row number containing maximum ones. In case of more than one row, then return smallest row number. # Example 1 input: [ [0, 1], [1, 0], ], # Expected output: 1 (Row 1 and Row 2 have the same number of ones, so return 1.) # Example 2 input: [ [0, 0, 0], [1, 0, 1], ], # Expected output: 2 (Row 2 has the maximum ones, so return 2.) # Example 3 input: [ [0, 0], [1, 1], [0, 0], ], # Expected output: 2 (Row 2 have the maximum ones, so

Robbie Hatley's Solutions To The Weekly Challenge #270

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-19 through 2024-05-25 is #270. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 270-1: Special Positions Submitted by: Mohammad Sajid Anwar You are given a m x n binary matrix. Write a script to return the number of special positions in the given binary matrix. A position (i, j) is called "special" if $matrix[i][j] == 1 and all other elements in the row i and column j are 0. Example 1 input: [1, 0, 0], [0, 0, 1], [1, 0, 0], There is only one special position (1, 2) as $matrix[1][2] == 1 and all other elements in row 1 and column 2 are 0. Expected output: 1 Example 2 input: [1, 0, 0], [0, 1, 0], [0, 0, 1], Special positions are (0,0), (1, 1) and (2,2). Expected output: 3 The method I

Robbie Hatley's Solutions To The Weekly Challenge #269

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-12 through 2024-05-18 is #269. Its tasks are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 269-1: Bitwise OR Submitted by: Mohammad Sajid Anwar You are given an array of 2-or-more positive integers, @ints. Write a script to find out if it is possible to select two or more elements of @ints such that the bitwise OR of those elements has at least one trailing zero in its binary representation. Example 1 input: (1, 2, 3, 4, 5) Expected output: true Say we pick 2 and 4; their bitwise OR is 6. The binary representation of 6 is 110. Return true since we have one trailing zero. Example 2 input: (2, 3, 8, 16) Expected output: true Say we pick 2 and 8; their bitwise OR is 10. The binary representation of 10 is 1010. Return true sinc

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