diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-11-06 17:20:38 -0800 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-11-06 17:20:38 -0800 |
| commit | 6f877c5c2cf919fcd759f0e45adccdea67f45ac8 (patch) | |
| tree | a312bd46866f10bb262b5db9ba8fd0053e633fae | |
| parent | a82dd587d3773d2a36a1fcc4b3c525c031433a4a (diff) | |
| download | perlweeklychallenge-club-6f877c5c2cf919fcd759f0e45adccdea67f45ac8.tar.gz perlweeklychallenge-club-6f877c5c2cf919fcd759f0e45adccdea67f45ac8.tar.bz2 perlweeklychallenge-club-6f877c5c2cf919fcd759f0e45adccdea67f45ac8.zip | |
Robbie Hatley's solutions in Perl for The Weekly Challenge #242.
| -rw-r--r-- | challenge-242/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-242/robbie-hatley/perl/ch-1.pl | 142 | ||||
| -rwxr-xr-x | challenge-242/robbie-hatley/perl/ch-2.pl | 133 |
3 files changed, 276 insertions, 0 deletions
diff --git a/challenge-242/robbie-hatley/blog.txt b/challenge-242/robbie-hatley/blog.txt new file mode 100644 index 0000000000..5d4a917dba --- /dev/null +++ b/challenge-242/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/11/robbie-hatleys-solutions-to-weekly.html
\ No newline at end of file diff --git a/challenge-242/robbie-hatley/perl/ch-1.pl b/challenge-242/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..6adfe22605 --- /dev/null +++ b/challenge-242/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,142 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +-------------------------------------------------------------------------------------------------------------- +TITLE BLOCK: +Solutions in Perl for The Weekly Challenge 242-1. +Written by Robbie Hatley on Mon Nov 06, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: + +Task 1: Missing Members +Submitted by: Mohammad S Anwar +Given two arrays of integers, write a script to find the [unique] +members of each array which are missing in the other array. + +Example 1: +Input: @arr1 = (1, 2, 3); @arr2 = (2, 4, 6); +Output: ([1, 3], [4, 6]) +(1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +(2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). + +Example 2: +Input: @arr1 = (1, 2, 3, 3); @arr2 = (1, 1, 2, 2); +Output: ([3], []) +(1, 2, 3, 3) has 2 members (3, 3) missing in the array +(1, 1, 2, 2). Since they are same, keep just one. +(1, 1, 2, 2) has 0 members missing in the array (1, 2, 3, 3). + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: + +The original problem description didn't specify the word [unique], but Example 2 makes it clear that we are +looking for unique (no repeats) members of each array which are missing in the other. So I'll first make +"unique" versions of each array by using the "uniq" function from List::Util and the built-in "sort" function: +use List::Util 'uniq'; +@unique1 = uniq sort @arr1; +@unique2 = uniq sort @arr2; +Then I'll just see which elements of @unique1 aren't in @unique2 and vice-versa. I'll make subs "is_int", +"are_ints", "is_in", and "missing" to do most of the work, then call these from the main array loop: + +-------------------------------------------------------------------------------------------------------------- +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 two arrays of integers, in proper Perl syntax, like so: +./ch-1.pl '([[-3,8,42,7,11],[6,47,42,11,9]],[[3,5,7,9],[2,4,6,8]],[[2,3,5,8],[2,3,5,8]])' + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; +use List::Util 'uniq'; + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +our $t0; +BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +# Is a given scalar a decimal representation of an integer? +sub is_int ($x) { + return $x =~ m/^-[1-9]\d*$|^0$|^[1-9]\d*$/; +} + +# Does a referred-to array contain only decimal representations of integers? +sub are_ints ($aref) { + for (@$aref){return 0 if !is_int($_)} + return 1; +} + +# Is a given element in a referred-to array? +sub is_in ($x, $aref) { + for (@$aref){return 1 if $_ == $x} + return 0; +} + +# Return an array of two arrays of elements which are "missing" +# relative to a pair of referred-to arrays: +sub missing ($aref1, $aref2) { + my @unique1 = uniq sort {$a<=>$b} @$aref1; + my @unique2 = uniq sort {$a<=>$b} @$aref2; + my @missing1; + my @missing2; + for (@unique1){push @missing1, $_ if !is_in($_, \@unique2)} + for (@unique2){push @missing2, $_ if !is_in($_, \@unique1)} + return ([@missing1],[@missing2]); +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + #Example 1 Input: + [[1, 2, 3],[2, 4, 6]], + #Expected output: ([1, 3], [4, 6]) + + #Example 2 Input: + [[1, 2, 3, 3],[1, 1, 2, 2]], + #Expected output: ([3], []) +); + +# Main loop: +for my $aref (@arrays) { + say ''; + say 'Array1 = (' . join(', ', @{$$aref[0]}) . ')'; + say 'Array2 = (' . join(', ', @{$$aref[1]}) . ')'; + if ( !are_ints($$aref[0]) || !are_ints($$aref[1]) ) { + say 'Error: array pair contains non-integer elements;'; + say 'skipping to next array pair.'; + next; + } + my @missing = missing($$aref[0],$$aref[1]); + say 'Mssng1 = (' . join(', ', @{$missing[0]}) . ')'; + say 'Mssng2 = (' . join(', ', @{$missing[1]}) . ')'; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ diff --git a/challenge-242/robbie-hatley/perl/ch-2.pl b/challenge-242/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..8a47f66614 --- /dev/null +++ b/challenge-242/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,133 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +-------------------------------------------------------------------------------------------------------------- +TITLE BLOCK: +Solutions in Perl for The Weekly Challenge 242-2. +Written by Robbie Hatley on Mon Nov 06, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 2: Flip Matrix +Submitted by: Mohammad S Anwar +Given n x n binary matrix, write a script to flip +the given matrix as below: +1 1 0 +0 1 1 +0 0 1 +a) Reverse each row +0 1 1 +1 1 0 +1 0 0 +b) Invert each member +1 0 0 +0 0 1 +0 1 1 + +Example 1: +Input: ([1, 1, 0], [1, 0, 1], [0, 0, 0]) +Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) + +Example 2: +Input: ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]) +Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +Reversing the rows is easily done with "reverse", and logically-inverting the elements can be done with "!". +However, instead of doing that, I think I'll use a combined approach using ranged "for", "!", and "unshift": + +-------------------------------------------------------------------------------------------------------------- +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 arrays of elements, each of which are 0 or 1, in proper Perl syntax, like so: +./ch-2.pl '([[1,1,1],[0,0,0],[1,1,1]],[[1,0,0],[0,1,0],[0,0,1]])' + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +our $t0; +BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +# Is a referred-to array a matrix (array of arrays) of +# bools (elements which are "0" or "1")? +sub is_matrix_of_bools ($matref) { + return 0 if 'ARRAY' ne ref $matref; + for my $rowref (@$matref) { + return 0 if 'ARRAY' ne ref $rowref; + for my $element ( @{$rowref} ) { + return 0 if '0' ne $element && '1' ne $element; + } + } + return 1; +} + +# Flip-and-negate a matrix of bools: +sub flip_mat ($matref) { + my @flipped; + for my $rowref (@{$matref}) { + my @new_row = (); + unshift(@new_row, (!$_ ? '1' : '0')) for @{$rowref}; + push @flipped, [@new_row]; + } + return @flipped; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @matrices = @ARGV ? eval($ARGV[0]) : +( + # Example1 input: + [[1, 1, 0], [1, 0, 1], [0, 0, 0]], + # Expected output: + # ([1, 0, 0], [0, 1, 0], [1, 1, 1]) + + # Example2 input: + [[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]], + # Expected output: + # ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) +); + +# Main loop: +for my $matref (@matrices) { + say ''; + say 'Matrix = '; + say join(', ', @{$_}) for @{$matref}; + if (!is_matrix_of_bools($matref)) { + say 'Error: Not a matrix of bools. Skipping to next matrix.'; + next; + } + my @flipped = flip_mat($matref); + say 'Flipped = '; + say join(', ', @{$_}) for @flipped; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ |
