### 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 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 used was, I first made lists of sums-of-elements for all rows and columns, then I counted elements for which value, list-sum, and column-sum are all 1:

Robbie Hatley's Perl Solution to The Weekly Challenge 270-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 270-2: Equalize Array Submitted by: Mohammad Sajid Anwar You are give an array of integers, @ints and two integers, $x and $y. Write a script to execute one of the two options: Level 1: Pick an index i of the given array and do $ints[i] += 1 Level 2: Pick two different indices i,j and do $ints[i] +=1 and $ints[j] += 1. You are allowed to perform as many levels as you want to make every elements in the given array equal. There is cost attached for each level. For Level 1 the cost is $x, and $y for Level 2. In the end return the minimum cost to get the work done. Example 1: Input: @ints = (4, 1), $x = 3 and $y = 2 Level 1: i=1, so $ints[1] += 1. @ints = (4, 2) Level 1: i=1, so $ints[1] += 1. @ints = (4, 3) Level 1: i=1, so $ints[1] += 1. @ints = (4, 4) We perforned operation Level 1, 3 times. So the total cost would be 3 x $x => 3 x 3 => 9 Expected output: 9 Example 2: Input: @ints = (2, 3, 3, 3, 5), $x = 2 and $y = 1 Level 2: i=0, j=1, so $ints[0] += 1 and $ints[1] += 1 @ints = (3, 4, 3, 3, 5) Level 2: i=0, j=2, so $ints[0] += 1 and $ints[2] += 1 @ints = (4, 4, 4, 3, 5) Level 2: i=0, j=3, so $ints[0] += 1 and $ints[3] += 1 @ints = (5, 4, 4, 4, 5) Level 2: i=1, j=2, so $ints[1] += 1 and $ints[2] += 1 @ints = (5, 5, 5, 4, 5) Level 1: i=3, so $ints[3] += 1 @ints = (5, 5, 5, 5, 5) We perforned operation Level 1, 1 time and Level 2, 4 times. So the total cost would be (1 x $x) + (3 x $y) => (1 x 2) + (4 x 1) => 6 Expected output: 6

Sorry, but I didn't have the time or energy this week to solve this, so you're on your own on this one.

That's it for challenge 270; see you on challenge 271!

## Comments

## Post a Comment