Perl Weekly Challenge 217.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 217.
Task 1: Sorted Matrix
Submitted by: Mohammad S Anwar
You are given a n x n matrix where n >= 2.
Write a script to find 3rd smallest element in the sorted matrix.
Example 1
Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3])
Output: 1
The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5.
The 3rd smallest of the sorted list is 1.
Example 2
Input: @matrix = ([2, 1], [4, 5])
Output: 4
The sorted list of the given matrix: 1, 2, 4, 5.
The 3rd smallest of the sorted list is 4.
Example 3
Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1])
Output: 0
The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3.
The 3rd smallest of the sorted list is 0.
Curiously, the fact that the input is a square matrix is never used, just its contents. Nevertheless, I use the Perl Data Language PDL to easily manipulate the matrices, yielding a short oneliner
Example 1
perl -MPDL -E 'for(@ARGV){$x=pdl($_);say "$x->", $x->flat->qsort->at(2)}' '[[3,1,2],[5,2,4],[0,1,3]]'
Results:
[
[3 1 2]
[5 2 4]
[0 1 3]
]
->1
Other examples:
perl -MPDL -E 'for(@ARGV){$x=pdl($_);say "$x->", $x->flat->qsort->at(2)}' \
'[[2,1],[4,5]]' '[[1,0,3],[0,0,0],[1,2,1]]'
Results:
[
[2 1]
[4 5]
]
->4
[
[1 0 3]
[0 0 0]
[1 2 1]
]
->0
The full code is almost identical.
1 # Perl weekly challenge 217
2 # Task 1: Sorted Matrix
3 #
4 # See https://wlmb.github.io/2023/05/15/PWC217/#task-1-sorted-matrix
5 use v5.36;
6 use PDL;
7 use experimental "try";
8 die <<~"FIN" unless @ARGV;
9 Usage: $0 '[[m11, m22, ...],[m21,m22,...],...]'
10 to find the third element of the sorted matrix elements mij.
11 FIN
12 for(@ARGV){
13 try {
14 my $x=pdl($_); # Convert input string to pdl-ndarray
15 die "Expected a square matrix: $_"
16 unless $x->isa("PDL") and $x->dims==2 and $x->dim(0)==$x->dim(1);
17 my $result=$x
18 ->flat # convert to vector
19 ->qsort # sort its elements
20 ->at(2); # choose third smallest
21 say "$x->$result";
22 }
23 catch($e){
24 warn "$e";
25 }
26 }
Example:
./ch-1.pl 'jaja' '[[3,1,2],[5,2,4],[0,1,3]]' '[[2,1],[4,5]]' '[[1,0,3],[0,0,0],[1,2,1]]'
Results:
[
[3 1 2]
[5 2 4]
[0 1 3]
]
->1
[
[2 1]
[4 5]
]
->4
[
[1 0 3]
[0 0 0]
[1 2 1]
]
->0
Task 2: Max Number
Submitted by: Mohammad S Anwar
You are given a list of positive integers.
Write a script to concatenate the integers to form the highest possible value.
Example 1:
Input: @list = (1, 23)
Output: 231
Example 2:
Input: @list = (10, 3, 2)
Output: 3210
Example 3:
Input: @list = (31, 2, 4, 10)
Output: 431210
Example 4:
Input: @list = (5, 11, 4, 1, 2)
Output: 542111
Example 5:
Input: @list = (1, 10)
Output: 110
A simple solution consists on sorting the input numbers according to
their pairwise concatenation, $a
goes before $b
if the
concatenation $a$b
has a larger numerical value than the concatenation
$b$a
. This yields a short oneliner:
Example 1:
perl -E 'say "@ARGV -> ", join "", sort {"$b$a" <=> "$a$b"} @ARGV' 1 23
Results:
1 23 -> 231
Other examples:
perl -E 'say "@ARGV -> ", join "", sort {"$b$a" <=> "$a$b"} @ARGV' 10 3 2
perl -E 'say "@ARGV -> ", join "", sort {"$b$a" <=> "$a$b"} @ARGV' 31 2 4 10
perl -E 'say "@ARGV -> ", join "", sort {"$b$a" <=> "$a$b"} @ARGV' 5 11 4 1 2
perl -E 'say "@ARGV -> ", join "", sort {"$b$a" <=> "$a$b"} @ARGV' 1 10
Results:
10 3 2 -> 3210
31 2 4 10 -> 431210
5 11 4 1 2 -> 542111
1 10 -> 110
The full code is similar:
1 # Perl weekly challenge 217
2 # Task 2: Max Number
3 #
4 # See https://wlmb.github.io/2023/05/15/PWC217/#task-2-max-number
5 use v5.36;
6 use List::Util qw(all);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 N1 [N2...]
9 to build the highest possible number by rearranging and concatenating
10 the numbers N1 N2...
11 FIN
12 die "Expected nonnegative integers" unless all {/^\d+$/} @ARGV;
13 say "@ARGV -> ", join "", sort {"$b$a" <=> "$a$b"} @ARGV;
Examples:
./ch-2.pl 10 3 2
./ch-2.pl 31 2 4 10
./ch-2.pl 5 11 4 1 2
./ch-2.pl 1 10
Results:
10 3 2 -> 3210
31 2 4 10 -> 431210
5 11 4 1 2 -> 542111
1 10 -> 110
Written on May 15, 2023