Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #354 (“Min Abs Diff” and “Shift Grid”)

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-12-29 through 2026-01-04 is #354. The tasks for challenge #354 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 354-1: Min Abs Diff
Submitted by: Mohammad Sajid Anwar
You are given an array of distinct integers. Write a script to
find all pairs of elements with the minimum absolute difference.
Rules (a,b):
1: a, b are from the given array.
2: a < b
3: b - a = min abs diff any two elements in the given array

# Example inputs and corresponding expected outputs:
(
   # Example #1 input:
   [4, 2, 1, 3],
   # Expected output: [1, 2], [2, 3], [3, 4]

   # Example #2 input:
   [10, 100, 20, 30],
   # Expected output: [10, 20], [20, 30]

   # Example #3 input:
   [-5, -2, 0, 3],
   # Expected output: [-2, 0]

   # Example #4 input:
   [8, 1, 15, 3],
   # Expected output: [1, 3]

   # Example #5 input:
   [12, 5, 9, 1, 15],
   # Expected output: [9, 12], [12, 15]
);

To solve this problem, I make a hash of all possible pairs ($x,$y) keyed by abs($x-$y), then I simply return those pairs associated with the minimum key. The "min" function from "List::Util" is handy for this. I also sort elements within each pair in ascending order before hashing, and I sort the returned pairs in ascending order of first element.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 354-2: Shift Grid
Submitted by: Mohammad Sajid Anwar
You are given m x n matrix and an integer, $k. Write a script to
right-shift the given matrix by $k positions.

Each shift follows these rules:

Rule 1: Element at grid[i][j] moves to grid[i][j + 1]. This means
every element moves one step to the right within its row.

Rule 2: Element at grid[i][n - 1] moves to grid[i + 1][0]
This handles the last column: elements in the last column of
row i wrap to the first column of the next row (i+1).

Rule 3: Element at grid[m - 1][n - 1] moves to grid[0][0]
This is the bottom-right corner: it wraps to the top-left corner.

# Example inputs and corresponding expected outputs:
(
   # Example 1 input:
   [
      [
         [1, 2, 3],
         [4, 5, 6],
         [7, 8, 9],
      ],
      1,
   ],
   # Expected output:
   # 9, 1, 2
   # 3, 4, 5
   # 6, 7, 8

   # Example 2 input:
   [
      [
         [10, 20],
         [30, 40],
      ],
      1,
   ],
   # Expected output:
   # 40, 10
   # 20, 30

   # Example 3 input:
   [
      [
         [1, 2],
         [3, 4],
         [5, 6],
      ],
      1,
   ],
   # Expected output:
   # 6, 1
   # 2, 3
   # 4, 5

   # Example 4 input:
   [
      [
         [1, 2, 3],
         [4, 5, 6],
      ],
      5,
   ],
   # Expected output:
   # 2, 3, 4
   # 5, 6, 1

   # Example 5 input:
   [
      [
         [1, 2, 3, 4],
      ],
      1,
   ],
   # Expected output:
   # 4, 1, 2, 3
);

To solve this problem, I first make note of the shape of the matrix, then flatten it into one long list, then shift the list, then reformat it back to a matrix of the original dimensions.

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

That's it for challenge 354; see you on challenge 355!

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”)