Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #337 (“Count LE Others” and “Count Odds”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-09-01 through 2025-09-07 is #337

The tasks for challenge #337 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 337-1: Count LE Others
Submitted by: Mohammad Sajid Anwar
You are given an array of numbers, @num1. Write a script to
return an array, @num2, where $num2[$i] is the count of all
numbers in @num1 other than $num1[$i] which are less than or
equal to $num1[$i].

Example #1:
Input: @num1 = (6, 5, 4, 8)
Output: (2, 1, 0, 3)

Example #2:
Input: @num1 = (7, 7, 7, 7)
Output: (3, 3, 3, 3)

Example #3:
Input: @num1 = (5, 4, 3, 2, 1)
Output: (4, 3, 2, 1, 0)

Example #4:
Input: @num1 = (-1, 0, 3, -2, 1)
Output: (1, 2, 4, 0, 3)

Example #5:
Input: @num1 = (0, 1, 1, 2, 0)
Output: (1, 3, 3, 4, 1)

For each element of @num1, I'll use "grep" to find all elements of @num1 which are less-than-or-equal-to the current element, then use "scalar" to count that list, then subtract 1 to disqualify the current element, then push the resulting number to @num2. I'll then return @num2.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 337-2: Count Odds
Submitted by: Mohammad Sajid Anwar
You are given the height h and width w of a matrix of zeros
and a list of 0-indexed positions (r,c) within the matrix.
Write a script which performs the following two action for
each given location [r,c]:
a) Increment by 1 all the cells on row r.
b) Increment by 1 all the cells on column c.
Then return the count of odd integers in the matrix.

Example #1:
Input: $h = 2, $w = 3, @locations = ([0,1],[1,1])
Output: 6

Example #2:
Input: $h = 2, $w = 2, @locations = ([1,1],[0,0])
Output: 0

Example #3:
Input: $h = 3, $w = 3, @locations = ([0,0],[1,2],[2,1])
Output: 0

Example #4:
Input: $h = 1, $w = 5, @locations = ([0,2],[0,4])
Output: 2

Example #5:
Input: $h = 4, $w = 2, @locations = ([1,0],[3,1],[2,0],[0,1])
Output: 8

For a change, Task 2 in this challenge is much easier than Task 1, in that we simply need to do the things mentioned in the program description:

  1. Make a matrix of zeros of the given dimensions.
  2. For each coordinate pair (r,c), increment row r and column c.

Robbie Hatley's Perl Solution to The Weekly Challenge 337-2

That's it for challenge 337; see you on challenge 338!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)