Perl Weekly Challenge 391.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 391.
Task 1: Array Median
Submitted by: Mohammad Sajid Anwar
You are given two sorted arrays.
Write a script to merge the two given sorted arrays and
return the median of the merged array.
Example 1
Input: @arr1 = (2), @arr2 = (4)
Output: 3.0
Merged array: (2,4)
Median: (2+4)/2 => 3

Example 2
Input: @arr1 = (1,2,3), @arr2 = (7,8,9,10)
Output: 7.0
Merged array: (1,2,3,7,8,9,10)
Length of merged array is 7, the 4th element is 7.

Example 3
Input: @arr1 = (), @arr2 = (10,20,30,40)
Output: 25.0
Merged array: (10,20,30,40)
Median: (20+30)/2 => 25

Example 4
Input: @arr1 = (100), @arr2 = (1,2,3,4,5,6,7)
Output: 4.5
Merged array: (1,2,3,4,5,6,7,100)
Median: (4+5)/2 => 4.5

Example 5
Input: @arr1 = (1,2,2), @arr2 = (2,2,3)
Output: 2.0
Merged array: (1,2,2,2,2,3)
Median: (2+2)/2 => 2

The median is the value at the center of the sorted array, for odd
sized arrays, and the average of the values at both sides of
the center for even sized ones. A simple solution is to
actually build the merged array and average the two values
closest to the center (which may coincide, for odd sized
arrays). I assume the arrays are provided as space separated
strings in @ARGV. This yields a 1.5-liner:
Examples:
perl -E '
for my($x, $y)(@ARGV){@r=sort{$a<=>$b}map{split " ", $_}$x,$y;$n=floor((@r-1)/2);
say "$x; $y -> ", ($r[$n]+$r[@r-1-$n])/2;}
' "2" "4" "1 2 3" "7 8 9 10" "" "10 20 30 40" \
"100" "1 2 3 4 5 6 7" "1 2 2" "2 2 3"
Results:
2; 4 -> 3
1 2 3; 7 8 9 10 -> 7
; 10 20 30 40 -> 25
100; 1 2 3 4 5 6 7 -> 4.5
1 2 2; 2 2 3 -> 2
The full code is:
1 # Perl weekly challenge 391
2 # Task 1: Array Median
3 #
4 # See https://wlmb.github.io/2026/09/14/PWC391/#task-1-array-median
5 use v5.40;
6 die <<~"FIN" unless @ARGV and @ARGV%2==0;
7 Usage: $0 A0 B0 A1 B1...
8 to merge arrays An with Bn and print the median.
9 An Bn are provided as a string of space separated numbers.
10 FIN
11 for my($string1, $string2)(@ARGV){
12 my @merged = sort {$a<=>$b} map {split " ", $_} $string1, $string2;
13 my $middle=floor((@merged-1)/2);
14 say "$string1; $string2 -> ",
15 ($merged[$middle] + $merged[@merged - 1 - $middle])/2;
16 }
Example:
./ch-1.pl "2" "4" "1 2 3" "7 8 9 10" "" "10 20 30 40" \
"100" "1 2 3 4 5 6 7" "1 2 2" "2 2 3"
Results:
2; 4 -> 3
1 2 3; 7 8 9 10 -> 7
; 10 20 30 40 -> 25
100; 1 2 3 4 5 6 7 -> 4.5
1 2 2; 2 2 3 -> 2
Task 2: Arrange Box
Submitted by: Mohammad Sajid Anwar
You are given an array of box dimensions.
Write a script to determine the maximum number of these
boxes that can fit inside each other in a single stack. For
a box to fit inside another, it must be smaller in both
dimensions.
Example 1
Input: @boxes = ([1, 3], [3, 5], [6, 8], [2, 4])
Output: 4
Sort by width ascending: ([1, 3], [2, 4], [3, 5], [6, 8])
Extract heights: [3, 4, 5, 8]
[1, 3] -> [2, 4] -> [3, 5] -> [6, 8]

Example 2
Input: @boxes = ([4, 5], [4, 6], [6, 7], [2, 3], [4, 3])
Output: 3
Sort by width ascending: ([2, 3], [4, 6], [4, 5], [4, 3], [6, 7])
Extract heights: (3, 6, 5, 3, 7)
[2, 3] -> [4, 5] -> [6, 7]

Example 3
Input: @boxes = ([5, 5], [5, 5], [5, 5])
Output: 1
Sort by width ascending: ([5, 5], [5, 5], [5, 5])
Extract heights: (5, 5, 5)
[5, 5]

Example 4
Input: @boxes = ([2, 100], [3, 200], [4, 300], [5, 50], [5, 400])
Output: 4
Sort by width ascending: ([2, 100], [3, 200], [4, 300], [5, 400], [5, 50])
Extract heights: (100, 200, 300, 400, 50)
[2, 100] -> [3, 200] -> [4, 300] -> [5, 400]

Example 5
Input: @boxes = ([10, 20], [15, 10], [20, 30], [12, 18], [16, 25])
Output: 3
Sort by width ascending: ([10, 20], [12, 18], [15, 10], [16, 25], [20, 30])
Extract heights: (20, 18, 10, 25, 30)
[15, 10] -> [16, 25] -> [20, 30]
I can order the boxes according to how many can be nested
around it and its size. For each box, I find the most deeply
nested box (the first) in which it fits, trying larger before smaller
boxes. Then, the result is the largest of all the nesting
values when I finish examining all boxes. I assume the
inputs are in @ARGV as space separated pairs, each comma
separated into width and height. The result fits a 3.5-liner.
Examples:
perl -E '
for(@ARGV){@p=sort b map{[split(","),1]}split" ";for$x(1..@p-1){for$y(0..$x-1){
next unless f($p[$x],$p[$y]);$p[$x]->[2]=$p[$y]->[2]+1;@p=sort b @p;last;}}
say "$_ -> $p[0]->[2]";}sub b{$b->[2]<=>$a->[2]||$b->[1]<=>$a->[1]||$b->[0]<=>$a->[0]
}sub f($x,$y){$y->[0]>$x->[0]&&$y->[1]>$x->[1]}
' "1,3 3,5 6,8 2,4" "4,5 4,6 6,7 2,3 4,3" "5,5 5,5 5,5" \
"2,100 3,200 4,300 5,50 5,400" "10,20 15,10 20,30 12,18 16,25"
Results:
1,3 3,5 6,8 2,4 -> 4
4,5 4,6 6,7 2,3 4,3 -> 3
5,5 5,5 5,5 -> 1
2,100 3,200 4,300 5,50 5,400 -> 4
10,20 15,10 20,30 12,18 16,25 -> 3
Notice that I repeatedly reorder the array, but only modifying the first part, not beyond the box considered in the outer loop.
For the full code, instead of fully sorting the array of boxes at each iteration, I find through a linear search from the beginning the position to insert the newly nested box.
1 # Perl weekly challenge 391
2 # Task 2: Arrange Box
3 #
4 # See https://wlmb.github.io/2026/09/14/PWC391/#task-2-arrange-box
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 S0 S1...
8 to find how many boxes in Sn may be nested.
9 Sn is a string of space separated pairs of numbers "P0 P1..."
10 where each pair Pn is of the form width,height
11 FIN
12 for(@ARGV){
13 my @box =
14 sort {by_nesting_height_width($a, $b)}
15 map { [ split(","),1] } # [width, height, nesting]
16 split " ";
17 for my $small(1..@box-1){
18 my $bsmall=$box[$small];
19 for my $large(0..$small-1){
20 next unless fits($bsmall, $box[$large]);
21 $bsmall->[2] = $box[$large]->[2]+1; # update nesting level
22 my $newplace = $small;
23 for (0..$small-1){
24 $newplace = $_, last
25 if by_nesting_height_width($bsmall, $box[$_]);
26 }
27 splice @box, $small, 1;
28 splice @box, $newplace, 0, $bsmall;
29 last;
30 }
31 }
32 say "$_ -> $box[0]->[2]";
33 }
34 sub by_nesting_height_width($x, $y) {
35 $y->[2] <=> $x->[2] # compare nesting
36 || $y->[1]<=>$x->[1] # height
37 || $y->[0]<=>$x->[0] # width
38 }
39 sub fits($x,$y){
40 $x->[0] < $y->[0] && $x->[1] < $y->[1]
41 }
Example:
./ch-2.pl "1,3 3,5 6,8 2,4" "4,5 4,6 6,7 2,3 4,3" "5,5 5,5 5,5" \
"2,100 3,200 4,300 5,50 5,400" "10,20 15,10 20,30 12,18 16,25"
Results:
1,3 3,5 6,8 2,4 -> 4
4,5 4,6 6,7 2,3 4,3 -> 3
5,5 5,5 5,5 -> 1
2,100 3,200 4,300 5,50 5,400 -> 4
10,20 15,10 20,30 12,18 16,25 -> 3
/;