aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2024-05-14 02:44:35 -0700
committerrobbie-hatley <Robbie.Hatley@gmail.com>2024-05-14 02:44:35 -0700
commit7c157f53d46ffbc11deaeb34c210744b1cf9cc04 (patch)
tree8dc4b88d4f8169954f96f9d09ebf177649fcd91f
parent02e309b3d451fff60404eb5ab3e539056f99ce0d (diff)
downloadperlweeklychallenge-club-7c157f53d46ffbc11deaeb34c210744b1cf9cc04.tar.gz
perlweeklychallenge-club-7c157f53d46ffbc11deaeb34c210744b1cf9cc04.tar.bz2
perlweeklychallenge-club-7c157f53d46ffbc11deaeb34c210744b1cf9cc04.zip
Robbie Hatley's Perl solutions for The Weekly Challenge #269.
-rw-r--r--challenge-269/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-269/robbie-hatley/perl/ch-1.pl108
-rwxr-xr-xchallenge-269/robbie-hatley/perl/ch-2.pl112
3 files changed, 221 insertions, 0 deletions
diff --git a/challenge-269/robbie-hatley/blog.txt b/challenge-269/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..00dcb082e3
--- /dev/null
+++ b/challenge-269/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/05/robbie-hatleys-solutions-to-weekly_14.html \ No newline at end of file
diff --git a/challenge-269/robbie-hatley/perl/ch-1.pl b/challenge-269/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..1e2ac1503a
--- /dev/null
+++ b/challenge-269/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,108 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 269-1,
+written by Robbie Hatley on Wed May 13, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 269-1: Bitwise OR
+Submitted by: Mohammad Sajid Anwar
+You are given an array of 2-or-more positive integers, @ints.
+Write a script to find out if it is possible to select two or
+more elements of @ints such that the bitwise OR of those
+elements has at least one trailing zero in its binary
+representation.
+
+Example 1 input: (1, 2, 3, 4, 5)
+Expected output: true
+Say we pick 2 and 4; their bitwise OR is 6.
+The binary representation of 6 is 110.
+Return true since we have one trailing zero.
+
+Example 2 input: (2, 3, 8, 16)
+Expected output: true
+Say we pick 2 and 8; their bitwise OR is 10.
+The binary representation of 10 is 1010.
+Return true since we have one trailing zero.
+
+Example 3 Input: (1, 2, 5, 7, 9)
+Expected output: false
+Say we pick any two of these; both right binary digits will be 1
+(because all numbers are odd), so the right binary digit of the
+bitwise OR will also be 1 (because 1^1 is 1).
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+The bitwise OR of two-or-more elements "has at least one trailing zero" if-and-only-if the bitwise OR
+is divisible by 2. (If it's also divisible by 4, it will have two trailing zeros. If it's also
+divisible by 8, it will have three trailing zeros. Etc.)
+
+The bitwise OR of n positive integers will be even if-and-only-if all of those integers are even.
+
+Therefore a subset of 2-or-more elements of the input such that the bitwise OR of those elements has rightmost
+digit 0 will exist if-and-only-if 2-or-more elements of the input are even.
+
+Therefore this problem is equivalent to asking "are 2-or-more elements even?". So we can just count
+elements $_ such that (0 == $_ % 2) and return "true" if the count is >= 2, otherwise return "false".
+
+--------------------------------------------------------------------------------------------------------------
+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 2-or-more positive integers, in proper Perl syntax, like so:
+./ch-1.pl '([3,8,-17,13,6,82,7],[17,2,23,4,5,11,7],[5,3,7,9,6,17,11])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, VARIABLES, AND SUBS:
+
+ use v5.38;
+ $" = ', ';
+
+ sub is_two_or_more_pos_ints ($aref) {
+ 'ARRAY' ne ref $aref and return 0;
+ scalar(@$aref) < 2 and return 0;
+ for my $x (@$aref) {
+ $x !~ m/^[1-9]\d*$/ and return 0;
+ }
+ return 1;
+ }
+
+ sub two_or_more_are_even (@a) {
+ (grep {0 == $_%2} @a) >= 2;
+ }
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 input:
+ [1, 2, 3, 4, 5],
+ # Expected output: true
+
+ # Example 2 input:
+ [2, 3, 8, 16],
+ # Expected output: true
+
+ # Example 3 input:
+ [1, 2, 5, 7, 9],
+ # Expected output: false
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ say "Array = (@$aref)";
+ !is_two_or_more_pos_ints($aref)
+ and say 'Error: Not an array of two-or-more positive integers.'
+ and say 'Moving on to next array.'
+ and next;
+ say 'Can form bitwise OR with trailing zero? ', two_or_more_are_even(@$aref)?'true':'false';
+}
diff --git a/challenge-269/robbie-hatley/perl/ch-2.pl b/challenge-269/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..437808c313
--- /dev/null
+++ b/challenge-269/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,112 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 269-2,
+written by Robbie Hatley on Wed May 13, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 269-2: Distribute Elements
+Submitted by: Mohammad Sajid Anwar
+You are given an array, @ints, of 2-or-more distinct integers.
+Write a script to distribute the elements as described below:
+1) Move the 1st element of @ints to a new array @arr1.
+2) Move the 2nd element of @ints to a new array @arr2.
+3) Repeatedly move the first element of @ints to @arr1 or @arr2,
+ depending on these criteria:
+ a) If the last element of @arr1 is greater than the last
+ element of @arr2, then move the first element of @ints
+ to the end of @arr1.
+ b) Otherwise, move the first element of @ints
+ to the end of @arr2.
+4) Once @ints is empty, return the concatenated arrays
+ (@arr1, @arr2).
+
+Example 1
+Input: @ints = (2, 1, 3, 4, 5)
+Expected output: (2, 3, 4, 5, 1)
+
+Example 2
+Input: @ints = (3, 2, 4)
+Expected output: (3, 4, 2)
+
+Example 3
+Input: @ints = (5, 4, 3 ,8)
+Expected output: (5, 3, 4, 8)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This is just a matter of using "shift" to repeatedly remove the first element of the input array, and using
+"push" to push it to the end of @arr1 or @arr2 (@arr1 if it has the greater last element, else @arr2),
+then returning (@arr1, @arr2).
+
+--------------------------------------------------------------------------------------------------------------
+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 2-or-more distinct integers, in proper Perl syntax, like so:
+./ch-2.pl '([3,17,"dog"],[3,8,-17,13,6,82,7],[1,2,3,4,5,6,7],[7,6,5,4,3,2,1])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, VARIABLES, AND SUBS:
+
+ use v5.38;
+ use List::MoreUtils 'duplicates';
+ $" = ', ';
+
+ sub is_two_or_more_distinct_ints ($aref) {
+ 'ARRAY' ne ref $aref and return 0;
+ scalar(@$aref) < 2 and return 0;
+ for my $element (@$aref) {
+ $element !~ m/^-[1-9]\d*$|^0$|^[1-9]\d*$/ and return 0;
+ }
+ duplicates @$aref and return 0;
+ return 1;
+ }
+
+ sub distribute_elements (@ints) {
+ my @arr1; push @arr1, shift @ints;
+ my @arr2; push @arr2, shift @ints;
+ while (@ints) {
+ my $x = shift @ints;
+ $arr1[-1] > $arr2[-1] and push @arr1, $x
+ or push @arr2, $x;
+ }
+ (@arr1, @arr2);
+ }
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [2, 1, 3, 4, 5],
+ # Expected output: (2, 3, 4, 5, 1)
+
+ # Example 2 Input:
+ [3, 2, 4],
+ # Expected output: (3, 4, 2)
+
+ # Example 3
+ [5, 4, 3 ,8],
+ # Expected output: (5, 3, 4, 8)
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ say "Original array = (@$aref)";
+ !is_two_or_more_distinct_ints($aref)
+ and say 'Not an array of 2-or-more distinct integers.'
+ and say 'Moving on to next array.'
+ and next;
+ my @distributed = distribute_elements(@$aref);
+ say "Distributed array = (@distributed)";
+}