Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #324 (“2D Array” and “Total XOR”)
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 for the week of 2025-06-02 through 2025-06-08 is #324
The tasks for challenge #324 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 324-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])
To reshape a 1D array into a 2D array using same contents, one should really use APL, in which case the entire program would only be a few characters long. But in Perl we don't have the ⍴ operator, so I'll have to make my own "ρ2" operator for reshaping any data structure into a 2D array.

Robbie Hatley's Perl Solution to The Weekly Challenge 324-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 324-2: Total XOR Submitted by: Mohammad Sajid Anwar You are given an array of integers [in the range 0-255]. 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
This problem, as originally written, did not contain the phrase "in the range 0-255", so it was too vague to be answered, because there is no fixed way to represent "any integer" using a fixed, finite number of binary bits. So I added the specification "[in the range 0-255]".
Now we are left with two issues:
Firstly, compute all subsets of our input set. I'll do this by using the "combine" function from CPAN module "Math::Combinatorics" to obtain all n-combinations of the input, for all values of n from 0 through the size of the input.
Secondly, compute the n-ary bit-wise XOR of each subset. I'll do this by making a subroutine which recursively applies Perl's "^" bitwise-XOR operator.

Robbie Hatley's Perl Solution to The Weekly Challenge 324-2
That's it for challenge 324; see you on challenge 325!
Comments
Post a Comment