diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-11-15 19:22:42 -0800 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-11-15 19:22:42 -0800 |
| commit | ffe717baec960741a3e67243425c673ec0c7653e (patch) | |
| tree | 9c02b46d303ce75dc702dd7eaa274cb2afd50ee5 /challenge-243 | |
| parent | 55325ce08c15b9645e46d2811a2041ba56472d42 (diff) | |
| parent | 864c5d0ad153cd0397b8a075231203cce336c3c1 (diff) | |
| download | perlweeklychallenge-club-ffe717baec960741a3e67243425c673ec0c7653e.tar.gz perlweeklychallenge-club-ffe717baec960741a3e67243425c673ec0c7653e.tar.bz2 perlweeklychallenge-club-ffe717baec960741a3e67243425c673ec0c7653e.zip | |
Merge remote-tracking branch 'origin/243' into 243
Diffstat (limited to 'challenge-243')
| -rwxr-xr-x | challenge-243/robbie-hatley/perl/ch-1.pl | 124 | ||||
| -rwxr-xr-x | challenge-243/robbie-hatley/perl/ch-2.pl | 123 |
2 files changed, 0 insertions, 247 deletions
diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl deleted file mode 100755 index 20f03c220f..0000000000 --- a/challenge-243/robbie-hatley/perl/ch-1.pl +++ /dev/null @@ -1,124 +0,0 @@ -#!/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_pair_indices ($aref) { - my @rpi = (); - for my $i ( 0 .. $#$aref - 1 ) { - for my $j ( $i + 1 .. $#$aref - 0 ) { - if ( $$aref[$i] > 2 * $$aref[$j] ) { - push @rpi, [$i, $j]; - } - } - } - return @rpi -} - -# ------------------------------------------------------------------------------------------------------------ -# 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 @rpi = reverse_pair_indices($aref); - my $n = scalar @rpi; - say "Found $n reverse pairs", $n ? ':' : '.'; - for my $pair (@rpi) { - say 'Array[', $$pair[0], ', ', $$pair[1], '] = (', $$aref[$$pair[0]], ', ', $$aref[$$pair[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-243/robbie-hatley/perl/ch-2.pl b/challenge-243/robbie-hatley/perl/ch-2.pl deleted file mode 100755 index 583fe8d6dc..0000000000 --- a/challenge-243/robbie-hatley/perl/ch-2.pl +++ /dev/null @@ -1,123 +0,0 @@ -#!/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-2.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 .. $#$aref ) { - for my $j ( 0 .. $#$aref ) { - $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__ |
