aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobbie-hatley <Hatley.Software@gmail.com>2024-09-11 09:14:02 -0700
committerrobbie-hatley <Hatley.Software@gmail.com>2024-09-11 09:14:02 -0700
commit6c56e3243c6aa3ff4ec0c99d7b9b864a235ca32e (patch)
tree128bef445ef3c5d8a412a1426cb404099571fbf6
parent6026a29f10d144fe7933cb47dc7e3aebf624a997 (diff)
downloadperlweeklychallenge-club-6c56e3243c6aa3ff4ec0c99d7b9b864a235ca32e.tar.gz
perlweeklychallenge-club-6c56e3243c6aa3ff4ec0c99d7b9b864a235ca32e.tar.bz2
perlweeklychallenge-club-6c56e3243c6aa3ff4ec0c99d7b9b864a235ca32e.zip
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #286.
-rw-r--r--challenge-286/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-286/robbie-hatley/perl/ch-1.pl10
-rwxr-xr-xchallenge-286/robbie-hatley/perl/ch-2.pl117
3 files changed, 128 insertions, 0 deletions
diff --git a/challenge-286/robbie-hatley/blog.txt b/challenge-286/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..f873ac2489
--- /dev/null
+++ b/challenge-286/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/09/robbie-hatleys-solutions-to-weekly_11.html \ No newline at end of file
diff --git a/challenge-286/robbie-hatley/perl/ch-1.pl b/challenge-286/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..6dec583185
--- /dev/null
+++ b/challenge-286/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,10 @@
+#!/usr/bin/env perl
+$/ = undef;
+my $fh = undef;
+my $source;
+open $fh, "<", "$0";
+read $fh, $source, -s $0;
+close $fh;
+my @tokens = split /\s+/, $source;
+my $token = $tokens[int rand scalar @tokens];
+print "$token\n";
diff --git a/challenge-286/robbie-hatley/perl/ch-2.pl b/challenge-286/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..eeaeb1b789
--- /dev/null
+++ b/challenge-286/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,117 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 286-2,
+written by Robbie Hatley on Wed Sep 11, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 286-2: Order Game
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints, whose length is a
+power of 2. Write a script to play the order game (min and max)
+and return the last element.
+
+Example 1
+Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2)
+Output: 1
+Operation 1:
+min(2, 1) = 1
+max(4, 5) = 5
+min(6, 3) = 3
+max(0, 2) = 2
+Operation 2:
+min(1, 5) = 1
+max(3, 2) = 3
+Operation 3:
+min(1, 3) = 1
+
+Example 2
+Input: @ints = (0, 5, 3, 2)
+Output: 0
+Operation 1:
+min(0, 5) = 0
+max(3, 2) = 3
+Operation 2:
+min(0, 3) = 0
+
+Example 3
+Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8)
+Output: 2
+Operation 1:
+min(9, 2) = 2
+max(1, 4) = 4
+min(5, 6) = 5
+max(0, 7) = 7
+min(3, 1) = 1
+max(3, 5) = 5
+min(7, 9) = 7
+max(0, 8) = 8
+Operation 2:
+min(2, 4) = 2
+max(5, 7) = 7
+min(1, 5) = 1
+max(7, 8) = 8
+Operation 3:
+min(2, 7) = 2
+max(1, 8) = 8
+Operation 4:
+min(2, 8) = 2
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I could use one of the "pairwise" functions from one of the "List::Xxxxxxx" CPAN modules, but in this case
+it's simpler to use recursion, reducing the "condensed" size of the list by a factor of 2 at each level.
+
+--------------------------------------------------------------------------------------------------------------
+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 integers, in proper Perl syntax. Each inner array should have a length which
+is a power of 2. For example:
+./ch-2.pl '([1,2,3,4,5,6,7,8],[8,7,6,5,4,3,2,1])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+use List::Util qw( min max );
+sub condense {
+ my @array = @_;
+ my @condensed;
+ my $max = 0;
+ for ( my $i = 0 ; $i <= $#array-1 ; $i += 2 ) {
+ $max
+ ? push(@condensed, max($array[$i],$array[$i+1]))
+ : push(@condensed, min($array[$i],$array[$i+1]));
+ $max = !$max;
+ }
+ 1 == scalar(@condensed)
+ ? return $condensed[0]
+ : return condense(@condensed);
+}
+
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ [2, 1, 4, 5, 6, 3, 0, 2],
+ [0, 5, 3, 2],
+ [9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8],
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ my @array = @$aref;
+ my $condensed = condense(@array);
+ print "\n";
+ print "Array = (@array)\n";
+ print "Order-Game Result = $condensed\n";
+}