Perl Weekly Challenge 324.

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

Task 1: 2D Array

Submitted by: Mohammad Sajid Anwar
You are given an array of integers and two integers $r amd $c.

Write a script to create two dimension array having $r rows and $c columns using the given array.


Example 1
Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2
Output: ([1, 2], [3, 4])

Example 2
Input: @ints = (1, 2, 3), $r = 1, $c = 3
Output: ([1, 2, 3])

Example 3
Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1
Output: ([1], [2], [3], [4])

The Perl Data Language PDL has operators to reshape arrays, making this problem quite trivial.

Example 1:

perl -MPDL -E '
($r, $c, @i)=@ARGV; say "r=$r, c=$c, array=", pdl(@i), "->", pdl(@i)->reshape($c,$r)
' 2 2 1 2 3 4

Results:

r=2, c=2, array=[1 2 3 4]->
[
 [1 2]
 [3 4]
]

Example 2:

perl -MPDL -E '
($r, $c, @i)=@ARGV; say "r=$r, c=$c, array=", pdl(@i), "->", pdl(@i)->reshape($c,$r)
' 1 3 1 2 3

Results:

r=1, c=3, array=[1 2 3]->
[
 [1 2 3]
]

Example 3:

perl -MPDL -E '
($r, $c, @i)=@ARGV; say "r=$r, c=$c, array=", pdl(@i), "->", pdl(@i)->reshape($c,$r)
' 4 1 1 2 3 4

Results:

r=4, c=1, array=[1 2 3 4]->
[
 [1]
 [2]
 [3]
 [4]
]

The full code is similar:

 1  # Perl weekly challenge 324
 2  # Task 1:  2D Array
 3  #
 4  # See https://wlmb.github.io/2025/06/02/PWC324/#task-1-2d-array
 5  use v5.36;
 6  use PDL;
 7  die <<~"FIN" unless @ARGV >= 3;
 8      Usage: $0 R C D1 D2...
 9      to reshape the array (D1 D2...) into a matrix with
10      R rows and C columns
11      FIN
12  my ($rows, $cols, @array)=@ARGV;
13  die "Number of elements should equal RxC" unless $rows*$cols==@array;
14  my $array=pdl(@array);
15  my $reshaped=$array->copy->reshape($cols, $rows);
16  say "rows=$rows, cols=$cols, array= $array -> $reshaped";

Example:

./ch-1.pl 2 2 1 2 3 4
./ch-1.pl 1 3 1 2 3
./ch-1.pl 4 1 1 2 3 4

Results:

rows=2, cols=2, array= [1 2 3 4] ->
[
 [1 2]
 [3 4]
]

rows=1, cols=3, array= [1 2 3] ->
[
 [1 2 3]
]

rows=4, cols=1, array= [1 2 3 4] ->
[
 [1]
 [2]
 [3]
 [4]
]

Task 2: Total XOR

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

Write a script to return the sum of total XOR for every subset of given array.


Example 1
Input: @ints = (1, 3)
Output: 6

Subset [1],    total XOR = 1
Subset [3],    total XOR = 3
Subset [1, 3], total XOR => 1 XOR 3 => 2

Sum of total XOR => 1 + 3 + 2 => 6

Example 2
Input: @ints = (5, 1, 6)
Output: 28

Subset [5],       total XOR = 5
Subset [1],       total XOR = 1
Subset [6],       total XOR = 6
Subset [5, 1],    total XOR => 5 XOR 1 => 4
Subset [5, 6],    total XOR => 5 XOR 6 => 3
Subset [1, 6],    total XOR => 1 XOR 6 => 7
Subset [5, 1, 6], total XOR => 5 XOR 1 XOR 6 => 2

Sum of total XOR => 5 + 1 + 6 + 4 + 3 + 7 + 2 => 28

Example 3
Input: @ints = (3, 4, 5, 6, 7, 8)
Output: 480

I’ll assume all integers are different, or that subset actually means sublist, so I don’t have to check for repetitions. I build a recursive routine that takes a list and returns the xor’s of all its sublists. If the array is empty, it returns 0. If not, it separates the first element, recurses over the remaining elements and returns a list with and without~xor~-ing the first element with the resulting list. Finally, I sum all the elements of the list. I use reduce and sum from List::Util. The results takes a one-liner.

Example 1:

perl -MList::Util=reduce,sum -E '
sub f(@x){@x?(f(@x[1..@x-1]), map {$x[0]^$_}f(@x[1..@x-1])):(0)}say "@ARGV -> ",sum f(@ARGV)
' 1 3

Results:

1 3 -> 6

Example 2:

perl -MList::Util=reduce,sum -E '
sub f(@x){@x?(f(@x[1..@x-1]), map {$x[0]^$_}f(@x[1..@x-1])):(0)}say "@ARGV -> ",sum f(@ARGV)
' 5 1 6

Results:

5 1 6 -> 28

Example 3:

perl -MList::Util=reduce,sum -E '
sub f(@x){@x?(f(@x[1..@x-1]), map {$x[0]^$_}f(@x[1..@x-1])):(0)}say "@ARGV -> ",sum f(@ARGV)
' 3 4 5 6 7 8

Results:

3 4 5 6 7 8 -> 480

The full code is similar:

 1  # Perl weekly challenge 324
 2  # Task 2:  Total XOR
 3  #
 4  # See https://wlmb.github.io/2025/06/02/PWC324/#task-2-total-xor
 5  use v5.36;
 6  use List::Util qw(reduce sum);
 7  sub f(@x){
 8      if(@x){
 9          my ($first, @rest)=@x;
10          my @reduced=f(@rest);
11          return @reduced, map{$first^$_}@reduced;
12      }
13      return (0);
14  }
15  say "@ARGV -> ", sum f(@ARGV);

Example:

./ch-2.pl 1 3
./ch-2.pl 5 1 6
./ch-2.pl 3 4 5 6 7 8

Results:

1 3 -> 6
5 1 6 -> 28
3 4 5 6 7 8 -> 480

/;

Written on June 2, 2025