diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-11-14 23:43:38 -0800 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-11-14 23:43:38 -0800 |
| commit | 928927686b98f953cedfbf54b325f5c9ea8c7cad (patch) | |
| tree | 806b432882ec66fe22c3be46d0e3ef2ebc571870 | |
| parent | d20e7296170b997b7e690a58b79156f6c81f1cd2 (diff) | |
| download | perlweeklychallenge-club-928927686b98f953cedfbf54b325f5c9ea8c7cad.tar.gz perlweeklychallenge-club-928927686b98f953cedfbf54b325f5c9ea8c7cad.tar.bz2 perlweeklychallenge-club-928927686b98f953cedfbf54b325f5c9ea8c7cad.zip | |
Robbie Hatley's Perl solutions for The Weekly Challenge #243.
| -rw-r--r-- | challenge-243/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-243/robbie-hatley/perl/ch-1.pl | 119 | ||||
| -rwxr-xr-x | challenge-243/robbie-hatley/perl/ch-2.pl | 122 |
3 files changed, 242 insertions, 0 deletions
diff --git a/challenge-243/robbie-hatley/blog.txt b/challenge-243/robbie-hatley/blog.txt new file mode 100644 index 0000000000..ca868bf554 --- /dev/null +++ b/challenge-243/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/11/robbie-hatleys-solutions-to-weekly_14.html
\ No newline at end of file diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..828d630b59 --- /dev/null +++ b/challenge-243/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,119 @@ +#!/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 243-1. +Written by Robbie Hatley on Tue Nov 14, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 1: Reverse Pairs +Submitted by: Mohammad S Anwar +You are given an array of integers. Write a script to return the +number of "reverse pairs" in the given array. A "reverse pair" +is a pair (i, j) obeying both of the following: +a) 0 <= i < j < nums.length and +b) nums[i] > 2 * nums[j]. + +Example 1: +Input: @nums = (1, 3, 2, 3, 1) +Output: 2 +(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 + +Example 2: +Input: @nums = (2, 4, 3, 5, 1) +Output: 3 +(1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 +(2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll use two three-part loops to check all pairs and find all "reverse pairs". + +-------------------------------------------------------------------------------------------------------------- +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 positive integers, in proper Perl syntax, like so: +./ch-1.pl '([10,7,4,6,2],[7,6,5,7,6,5])' + +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: + +sub are_pos_ints ($aref) { + return 0 if 'ARRAY' ne ref $aref; + for ( @$aref ) {return 0 unless $_ =~ m/^[1-9]\d*$/;} + return 1; +} + +sub reverse_pairs ($aref) { + my @rp = (); + for ( my $i = 0 ; $i <= $#$aref - 1 ; ++$i ) { + for ( my $j = $i + 1 ; $j <= $#$aref - 0 ; ++$j ) { + push @rp, [$$aref[$i], $$aref[$j]] if $$aref[$i] > 2 * $$aref[$j]; + } + } + return @rp +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 Input: + [1, 3, 2, 3, 1], + # Expected Output: 2 + + # Example 2 Input: + [2, 4, 3, 5, 1], + # Expected Output: 3 +); + +# Main loop: +for my $aref (@arrays) { + say ''; + say 'Array = (', join(', ', @$aref), ')'; + unless ( are_pos_ints($aref) ) { + say 'Error: Not array of positive ints; skipping to next array.'; + next; + } + my @reverse_pairs = reverse_pairs($aref); + say 'Found ', scalar(@reverse_pairs), ' reverse pairs:'; + say '(', join(', ', map {'['.join(', ', @$_).']'} @reverse_pairs), ')'; +} +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-243/robbie-hatley/perl/ch-2.pl b/challenge-243/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..cc410fa8b1 --- /dev/null +++ b/challenge-243/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,122 @@ +#!/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 243-2. +Written by Robbie Hatley on Tue Nov 14, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 2: Floor Sum +Submitted by: Mohammad S Anwar + +You are given an array of positive integers (>=1). Write a +script to return the sum of floor(nums[i] / nums[j]) where +0 <= i,j < nums.length. The floor() function returns the +integer part of the division. + +Example 1: +Input: @nums = (2, 5, 9) +Output: 10 +floor(2 / 5) = 0 +floor(2 / 9) = 0 +floor(5 / 9) = 0 +floor(2 / 2) = 1 +floor(5 / 5) = 1 +floor(9 / 9) = 1 +floor(5 / 2) = 2 +floor(9 / 2) = 4 +floor(9 / 5) = 1 + +Example 2: +Input: @nums = (7, 7, 7, 7, 7, 7, 7) +Output: 49 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll use two three-part loops to sum all floors of pair quotients. + +-------------------------------------------------------------------------------------------------------------- +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 positive integers, in proper Perl syntax, like so: +./ch-1.pl '([10,7,4,6,2],[7,6,5,7,6,5])' + +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: + +sub are_pos_ints ($aref) { + return 0 if 'ARRAY' ne ref $aref; + for ( @$aref ) {return 0 unless $_ =~ m/^[1-9]\d*$/;} + return 1; +} + +sub quotient_floor_sum ($aref) { + my $sum = 0; + for ( my $i = 0 ; $i <= $#$aref ; ++$i ) { + for ( my $j = 0 ; $j <= $#$aref ; ++$j ) { + $sum += int($$aref[$i]/$$aref[$j]); + } + } + return $sum; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 Input: + [2,5,9], + # Expected Output: 10 + + # Example 2 Input: + [7,7,7,7,7,7,7], + # Expected Output: 49 +); + +# Main loop: +for my $aref (@arrays) { + say ''; + say 'Array = (', join(', ', @$aref), ')'; + unless ( are_pos_ints($aref) ) { + say 'Error: Not array of positive ints; skipping to next array.'; + next; + } + my $sum = quotient_floor_sum($aref); + say 'Sum of floors of pair quotients = ', $sum; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ |
