Perl Weekly Challenge 381.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 381.

Task 1: Same Row Column

Submitted by: Mohammad Sajid Anwar

You are given a n x n matrix containing integers from 1 to
n.

Write a script to find if every row and every column
contains all the integers from 1 to n.

Example 1
Input: @matrix = ([1, 2, 3, 4],
                  [2, 3, 4, 1],
                  [3, 4, 1, 2],
                  [4, 1, 2, 3],)
Output: true

Example 2
Input: @matrix = ([1])
Output: true

Example 3
Input: @matrix = ([1, 2, 5],
                  [5, 1, 2],
                  [2, 5, 1],)
Output: false

Elements are out of range 1..3.

Example 4
Input: @matrix = ([1, 2, 3],
                  [1, 2, 3],
                  [1, 2, 3],)
Output: false

Example 5
Input: @matrix = ([1, 2, 3],
                  [3, 1, 2],
                  [3, 2, 1],)
Output: false


I use the Perl Data Language PDL to read and manipulate matrices. I can sort all rows and columns and compare them to the sequence 1..n, i.e., the result is true if the given matrix and its transpose, agree after sorting with the sequence. This takes a two-liner.

Examples:

perl -MPDL -E '
for(@ARGV){$m=pdl$_;$s=1+sequence($m->dim(0));say "$m-> ",
pdl(map{($_->qsort==$s)->all}$m, $m->transpose)->all?"T":"F";}
' "[[1 2 3 4][2 3 4 1][3 4 1 2][4 1 2 3]]" \
  "[[1]]" \
  "[[1 2 5][5 1 2][2 5 1]]" \
  "[[1 2 3][1 2 3][1 2 3]]" \
  "[[1 2 3][3 1 2][3 2 1]]"

Results:

[
 [1 2 3 4]
 [2 3 4 1]
 [3 4 1 2]
 [4 1 2 3]
]
-> T

[
 [1]
]
-> T

[
 [1 2 5]
 [5 1 2]
 [2 5 1]
]
-> F

[
 [1 2 3]
 [1 2 3]
 [1 2 3]
]
-> F

[
 [1 2 3]
 [3 1 2]
 [3 2 1]
]
-> F

The full code is:

 1  # Perl weekly challenge 381
 2  # Task 1:  Same Row Column
 3  #
 4  # See https://wlmb.github.io/2026/07/06/PWC381/#task-1-same-row-column
 5  use v5.36;
 6  use feature qw(try);
 7  use PDL;
 8  die <<~"FIN" unless @ARGV;
 9      Usage: $0 M0 M1...
10      to find if all rows and columns of the NxN matrix Mn contain
11      all the numbers 1..N.
12      Mn are strings that may be interpreted by PDL as matrices.
13      FIN
14  for(@ARGV){
15      try {
16          my $matrix = pdl $_;
17          die "Matrix must be square" unless
18              $matrix->ndims==2 && $matrix->dim(0)==$matrix->dim(1);
19          my $seq = 1 + sequence($matrix->dim(0));
20          say "$matrix-> ",
21              pdl(
22                  map{($_->qsort==$seq)->all}$matrix, $matrix->transpose
23              )->all?"True":"False";
24      }
25      catch($e) {warn $e;}
26  }

Example:

./ch-1.pl "[[1 2 3 4][2 3 4 1][3 4 1 2][4 1 2 3]]" \
          "[[1]]" \
          "[[1 2 5][5 1 2][2 5 1]]" \
          "[[1 2 3][1 2 3][1 2 3]]" \
          "[[1 2 3][3 1 2][3 2 1]]"

Task 2: Smaller Greater Element

Submitted by: Mohammad Sajid Anwar
You are given an array of integers.

Write a script to find the number of elements that have both
a strictly smaller and greater element in the given array.

Example 1
Input: @int = (2,4)
Output: 0

Not enough elements in the array.

Example 2
Input: @int = (1, 1, 1, 1)
Output: 0

Example 3
Input: @int = (1, 1, 4, 8, 12, 12)
Output: 2

The elements are 4 and 8.

Example 4
Input: @int = (3, 6, 6, 9)
Output: 2

Both instances of 6.

Example 5
Input: @int = (0, -5, 10, -2, 4)
Output: 3

The elements are 0, -2, and 4.

Though it may be overkill, I use the Perl Data Language PDL to read the arrays, find their minimum and maximum, selecting the elements that are not equal to those minimum and maximum and count the resulting number of elements. The result takes a one-liner.

Examples

perl -MPDL -E '
for(@ARGV){$p=pdl$_;($x,$y)=$p->minmax;say "$_ -> ",$p->where(($p!=$x)&($p!=$y))->nelem}
' "[2 4]" "[1 1 1 1]" "[1 1 4 8 12 12]" "[3 6 6 9]" "[0 -5 10 -2 4]"

Results:

[2 4] -> 0
[1 1 1 1] -> 0
[1 1 4 8 12 12] -> 2
[3 6 6 9] -> 2
[0 -5 10 -2 4] -> 3

The full code is:

 1  # Perl weekly challenge 381
 2  # Task 2:  Smaller Greater Element
 3  #
 4  # See https://wlmb.github.io/2026/07/06/PWC381/#task-2-smaller-greater-element
 5  use v5.36;
 6  use feature qw(try);
 7  use PDL;
 8  die <<~"FIN" unless @ARGV;
 9      Usage: $0 A0 A1...
10      to count the elements of the array An that are both strictly larger
11      and smaller than some other elements. An is a string that can be
12      fed to PDL as an array
13      FIN
14  for(@ARGV){
15      try {
16          my $array = pdl $_;
17          die "Expected a 1D array; $_" unless $array->ndims==1;
18          my ($min, $max) = $array->minmax;
19          say "$_ -> ", $array->where(($array != $min) & ($array != $max))->nelem;
20      }
21      catch($e){ warn $e; }
22  }

Example:

./ch-2.pl "[2 4]" "[1 1 1 1]" "[1 1 4 8 12 12]" "[3 6 6 9]" "[0 -5 10 -2 4]"

Results:

[2 4] -> 0
[1 1 1 1] -> 0
[1 1 4 8 12 12] -> 2
[3 6 6 9] -> 2
[0 -5 10 -2 4] -> 3

/;

Written on July 6, 2026