aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-244/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-244/robbie-hatley/perl/ch-1.pl133
-rwxr-xr-xchallenge-244/robbie-hatley/perl/ch-2.pl126
-rwxr-xr-xchallenge-244/robbie-hatley/perl/ch-2alt.pl124
4 files changed, 384 insertions, 0 deletions
diff --git a/challenge-244/robbie-hatley/blog.txt b/challenge-244/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..fa47c4d9c6
--- /dev/null
+++ b/challenge-244/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2023/11/robbie-hatleys-solutions-to-weekly_24.html \ No newline at end of file
diff --git a/challenge-244/robbie-hatley/perl/ch-1.pl b/challenge-244/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..f5a27bf69e
--- /dev/null
+++ b/challenge-244/robbie-hatley/perl/ch-1.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 244-1.
+Written by Robbie Hatley on Tue Nov 21, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 1: Count Smaller
+Submitted by: Mohammad S Anwar
+You are given an array of integers. Write a script to calculate
+the number of integers smaller than the integer at each index.
+
+Example 1:
+Input: @int = (8, 1, 2, 2, 3)
+Output: (4, 0, 1, 1, 3)
+For index = 0, count of elements less 8 is 4.
+For index = 1, count of elements less 1 is 0.
+For index = 2, count of elements less 2 is 1.
+For index = 3, count of elements less 2 is 1.
+For index = 4, count of elements less 3 is 3.
+
+Example 2:
+Input: @int = (6, 5, 4, 8)
+Output: (2, 1, 0, 3)
+
+Example 3:
+Input: @int = (2, 2, 2)
+Output: (0, 0, 0)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+Well, the obvious (mundane, prosaic) way is: for each element, riffle through the array and count smaller
+elements. But let's not do that. Instead, let's make a copy of the array sorted in increasing order. Then for
+each element of the original array, "number of smaller elements" can be found by counting elements of the
+sorted array until a "not smaller" element is found.
+
+--------------------------------------------------------------------------------------------------------------
+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, like so:
+./ch-1.pl '([17,54,-72,13,83],[5,4,3,2,1,0],[3,3,3,3,3,3],[13,-7,5,1,5,22])'
+
+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 given array an array of integers?
+sub is_array_of_ints($aref) {
+ return 0 if 'ARRAY' ne ref $aref;
+ for (@$aref) {
+ return 0 if !/^-[1-9]\d*$|^0$|^[1-9]\d*$/;
+ }
+ return 1;
+}
+
+# Given an array of one-or-more integers, return the array
+# of numbers of elements of the original array which are
+# smaller than each element of the original array:
+sub count_smaller ($aref) {
+ my @sorted = sort {$a<=>$b} @$aref;
+ my @smaller = ();
+ foreach my $x (@$aref) {
+ my $count = 0;
+ foreach my $y (@sorted) {
+ $y < $x and ++$count or last;
+ }
+ push @smaller, $count;
+ }
+ return @smaller;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Inputs:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [8, 1, 2, 2, 3],
+ # Expected Output: (4, 0, 1, 1, 3)
+
+ # Example 2 Input:
+ [6, 5, 4, 8],
+ # Expected Output: (2, 1, 0, 3)
+
+ # Example 3 Input:
+ [2, 2, 2],
+ # Expected Output: (0, 0, 0)
+);
+
+# Main loop:
+for my $aref (@arrays) {
+ say '';
+ say 'Original Array = (', join(', ', @$aref), ')';
+ !is_array_of_ints($aref)
+ and say 'Error: Not an array of ints. Moving on to next array.'
+ and next;
+ say 'Smaller Counts = (', join(', ', count_smaller($aref)), ')';
+}
+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-244/robbie-hatley/perl/ch-2.pl b/challenge-244/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..96a719f078
--- /dev/null
+++ b/challenge-244/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,126 @@
+#!/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 244-2.
+Written by Robbie Hatley on Tue Nov 21, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 2: Group Hero
+Submitted by: Mohammad S Anwar
+You are given an array of integers representing the strength.
+Write a script to return the sum of the powers of all possible
+combinations; power is defined as the square of the largest
+number in a sequence, multiplied by the smallest.
+
+Example 1:
+Input: @nums = (2, 1, 4)
+Output: 141
+Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
+Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
+Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
+Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
+Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
+Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
+Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16
+Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I was going to use CPAN module "Math::Combinatorics" on this, but then I realized, we're only looking at the
+minimum and maximum elements (exactly two elements) of each combination, so we should be able to "elide the
+middle" and look at pairs only, by first sorting the array in descending numeric order, then calculating the
+"power" for each (max,min) pair, then multiply by the total number of combinations having that (max,min)
+(which should be 2^(j-i-1) but not less than 1), then tally the results.
+
+--------------------------------------------------------------------------------------------------------------
+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, like so:
+./ch-2.pl '([4,8,16,32],[-3,45,-17,63,-54],[0,0,0,0,0])'
+
+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 given array an array of integers?
+sub is_array_of_ints($aref) {
+ return 0 if 'ARRAY' ne ref $aref;
+ for (@$aref) {
+ return 0 if !/^-[1-9]\d*$|^0$|^[1-9]\d*$/;
+ }
+ return 1;
+}
+
+# How many combinations are there with given index limits?
+sub cmbs ($i, $j) {
+ my $span = abs($j-$i);
+ if ( 0 == $span ) {return 1;}
+ else {return 2**($span-1);}
+}
+
+# What is the sum of the powers of all combinations?
+sub sum_pow_cmbs ($aref) {
+ my $totl = 0;
+ my @desc = sort {$b<=>$a} @$aref;
+ for ( my $i = 0 ; $i <= $#$aref ; ++$i ) {
+ for ( my $j = $i ; $j <= $#$aref ; ++$j ) {
+ $totl += cmbs($i, $j)*$desc[$i]*$desc[$i]*$desc[$j];
+ }
+ }
+ return $totl;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Inputs:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [2, 1, 4],
+ # Expected Output: 141
+);
+
+# Main loop:
+for my $aref (@arrays) {
+ say '';
+ say 'Array = (', join(',',@$aref), ')';
+ is_array_of_ints($aref)
+ or say 'Error: Not an array of ints. Moving on to next array.'
+ and next;
+ say 'Sum of powers of combinations = ', sum_pow_cmbs($aref);
+}
+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-244/robbie-hatley/perl/ch-2alt.pl b/challenge-244/robbie-hatley/perl/ch-2alt.pl
new file mode 100755
index 0000000000..842ebee33d
--- /dev/null
+++ b/challenge-244/robbie-hatley/perl/ch-2alt.pl
@@ -0,0 +1,124 @@
+#!/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 244-2.
+Written by Robbie Hatley on Tue Nov 21, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 2: Group Hero
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers representing the strength.
+Write a script to return the sum of the powers of all possible
+combinations; power is defined as the square of the largest
+number in a sequence, multiplied by the smallest.
+
+Example 1:
+Input: @nums = (2, 1, 4)
+Output: 141
+Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
+Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
+Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
+Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
+Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
+Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
+Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16
+Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I was going to use CPAN module "Math::Combinatorics" on this, but then I realized, we're only looking at the
+minimum and maximum elements (exactly two elements) of each combination, so we should be able to "elide the
+middle" and look at pairs only, by first sorting the array in descending numeric order, then calculating the
+"power" for each (max,min) pair, then multiply by the total number of combinations having that (max,min)
+(which should be 2^(j-i-1) but not less than 1), then tally the results.
+
+--------------------------------------------------------------------------------------------------------------
+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, like so:
+./ch-2alt.pl '([4,8,16,32],[-3,45,-17,63,-54],[0,0,0,0,0])'
+
+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 Math::Combinatorics;
+use List::Util ('min', 'max');
+
+# ------------------------------------------------------------------------------------------------------------
+# START TIMER:
+our $t0;
+BEGIN {$t0 = time}
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+
+# Is a given array an array of integers?
+sub is_array_of_ints($aref) {
+ return 0 if 'ARRAY' ne ref $aref;
+ for (@$aref) {
+ return 0 if !/^-[1-9]\d*$|^0$|^[1-9]\d*$/;
+ }
+ return 1;
+}
+
+# What is the sum of the powers of all combinations?
+sub sum_pow_cmbs ($aref) {
+ my $total = 0;
+ my $asize = scalar(@$aref);
+ my @combs = ();
+ foreach my $csize ( 1 .. $asize ) {
+ push @combs, combine($csize,@$aref);
+ }
+ foreach my $cref ( @combs ) {
+ $total += min(@$cref)*max(@$cref)*max(@$cref);
+ }
+ return $total;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Inputs:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [2, 1, 4],
+ # Expected Output: 141
+);
+
+# Main loop:
+for my $aref (@arrays) {
+ say '';
+ say 'Array = (', join(',',@$aref), ')';
+ is_array_of_ints($aref)
+ or say 'Error: Not an array of ints. Moving on to next array.'
+ and next;
+ say 'Sum of powers of combinations = ', sum_pow_cmbs($aref);
+}
+exit;
+
+# ------------------------------------------------------------------------------------------------------------
+# DETERMINE AND PRINT EXECUTION TIME:
+END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)}
+__END__