diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2025-06-02 02:01:10 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2025-06-02 02:01:10 -0700 |
| commit | 8c014ec9258682d74dbf08595d14cdc3ba177eb5 (patch) | |
| tree | 09512e4fad118184935c05aa0767d39491be17ab | |
| parent | b0159e77cc4e56da3a5a1c86d5769c652bcc887e (diff) | |
| download | perlweeklychallenge-club-8c014ec9258682d74dbf08595d14cdc3ba177eb5.tar.gz perlweeklychallenge-club-8c014ec9258682d74dbf08595d14cdc3ba177eb5.tar.bz2 perlweeklychallenge-club-8c014ec9258682d74dbf08595d14cdc3ba177eb5.zip | |
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #324.
| -rw-r--r-- | challenge-324/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-324/robbie-hatley/perl/ch-1.pl | 100 | ||||
| -rwxr-xr-x | challenge-324/robbie-hatley/perl/ch-2.pl | 115 |
3 files changed, 216 insertions, 0 deletions
diff --git a/challenge-324/robbie-hatley/blog.txt b/challenge-324/robbie-hatley/blog.txt new file mode 100644 index 0000000000..d30e34b2eb --- /dev/null +++ b/challenge-324/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/06/robbie-hatleys-solutions-in-perl-for.html diff --git a/challenge-324/robbie-hatley/perl/ch-1.pl b/challenge-324/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..22f4298df2 --- /dev/null +++ b/challenge-324/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,100 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 324-1, +written by Robbie Hatley on Dow Mon Dm, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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]) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +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. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of 3-element arrays, in proper Perl syntax. Each 3-element array must have a non-empty +array of printable scalars (characters, strings, integers, or real numbers) as its first element, a positive +integer (number of rows) as its second element, and a positive integer (number of columns) as its third +element. For example: + +./ch-1.pl ' ( [ ["pig", "cow", "horse", "sheep"], 2, 2 ] , [ [4,5,6,7,8], 7, 3 ] ) ' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + use List::MoreUtils 'arrayify'; + + # Reshape any data structure into a 2D array: + sub ρ2 ($aref, $r, $c) { + my @input = arrayify @$aref; # Flatten the contents of @$aref. + my $n = scalar @input; # Get number of items in input. + my @output; # Make an output array. + my ($i, $j, $k) = (0,0,0); # Make and initialize some indices. + for $i (0..$r-1) { # For each row of new array: + for $j (0..$c-1) { # For each column of new array: + while ($k >= $n) {$k-=$n} # Do bounds checking on input. + $output[$i]->[$j] = $input[$k]; # Copy element from input to output. + ++$k}} # Increment input index. + return @output} # Return output. + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example #1 inputs: + [[1, 2, 3, 4], 2, 2], + # Expected output: ([1, 2], [3, 4]) + + # Example #2 inputs: + [[1, 2, 3], 1, 3], + # Expected output: ([1, 2, 3]) + + # Example #3 inputs: + [[1, 2, 3, 4], 4, 1], + # Expected output: ([1], [2], [3], [4]) +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aaref (@arrays) { + say ''; + my $aref = $aaref->[0]; + my $r = $aaref->[1]; + my $c = $aaref->[2]; + my @array = ρ2($aref, $r, $c); + say "Input array = (@$aref)"; + say "New dimensions = $r by $c"; + say "Output array = "; + say "[@$_]" for @array; +} diff --git a/challenge-324/robbie-hatley/perl/ch-2.pl b/challenge-324/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..4edb8ea881 --- /dev/null +++ b/challenge-324/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,115 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 324-2, +written by Robbie Hatley on Dow Mon Dm, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: + +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. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of double-quoted strings, in proper Perl syntax, like so: + +./ch-2.pl '(["rat", "bat", "cat"],["pig", "cow", "horse"])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + use Math::Combinatorics 'combine'; + + # n-ary XOR: + sub NARY_XOR (@a) { + my $n = scalar @a; # Number of operands. + if (0 == $n) {return 0;} # 0 operands => return 0. + elsif (1 == $n) {return $a[0]} # 1 operand => return operand. + elsif (2 == $n) {return $a[0] ^ $a[1]} # 2 operands => return x ^ y + else { # For more-than-2 operands, + my $x = shift @a; # return the result of + return $x ^ NARY_XOR(@a)}} # FirstOperand ^ (XOR of remaining operands) + + # n-ary TOTAL_XOR: + sub TOTAL_XOR (@a) { + my $n = scalar @a; # n is the number of elements in set @a. + my @power_set; # The "power set" of a set @a is the set of all of its subsets. + for my $i (0..$n) { # The "power set" of a set @a is ALSO the set of all i-combinations + my @ncombs = combine($i,@a); # of elements of @a for all numbers i from 0 through n. + push @power_set, @ncombs} # The number of subsets in the power set is always 2^n. + my $total = 0; # Total of n-ary XORs of all elements of @power_set. + foreach my $aref (@power_set) { # For each subset in power set, + $total += NARY_XOR(@$aref)} # add its n-ary XOR to our total. + return $total} # Return total. + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + [1, 3], # Expected output = 6 + [5, 1, 6], # Expected output = 28 + [3, 4, 5, 6, 7, 8], # Expected output = 480 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + say "Array = (@$aref)"; + my $TXOR = TOTAL_XOR(@$aref); + say "TXOR = $TXOR"; +} |
