diff options
| author | LoneWolfiNTj <Hatley.Software@gmail.com> | 2023-01-10 17:07:07 -0800 |
|---|---|---|
| committer | LoneWolfiNTj <Hatley.Software@gmail.com> | 2023-01-10 17:07:07 -0800 |
| commit | 0bf9365bc07419b0d971425ca60bf7c8144eb8c2 (patch) | |
| tree | 629850a3169da77540e9f65dad7150218a25f265 /challenge-199 | |
| parent | 13e4a2c007d7a0aac2bd1b8a48aa28003d772dc5 (diff) | |
| download | perlweeklychallenge-club-0bf9365bc07419b0d971425ca60bf7c8144eb8c2.tar.gz perlweeklychallenge-club-0bf9365bc07419b0d971425ca60bf7c8144eb8c2.tar.bz2 perlweeklychallenge-club-0bf9365bc07419b0d971425ca60bf7c8144eb8c2.zip | |
Robbie Hatley's Perl solutions and blog for Weekly Challenge #199
Diffstat (limited to 'challenge-199')
| -rw-r--r-- | challenge-199/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-199/robbie-hatley/perl/ch-1.pl | 53 | ||||
| -rwxr-xr-x | challenge-199/robbie-hatley/perl/ch-2.pl | 69 |
3 files changed, 123 insertions, 0 deletions
diff --git a/challenge-199/robbie-hatley/blog.txt b/challenge-199/robbie-hatley/blog.txt new file mode 100644 index 0000000000..680c284e0d --- /dev/null +++ b/challenge-199/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/01/robbie-hatleys-perl-solutions-to-weekly.html
\ No newline at end of file diff --git a/challenge-199/robbie-hatley/perl/ch-1.pl b/challenge-199/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..a74526825b --- /dev/null +++ b/challenge-199/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,53 @@ +#! /usr/bin/perl + +# /d/rhe/PWCC/199/ch-1 +# "Good Pairs" + +=pod + +Task 1: Good Pairs +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. Write a script to find +the total count of "Good Pairs". A pair (i, j) is called "good" +if list[i] == list[j] and i < j. + +Example 1: Input: (1,2,3,1,1,3) Output: 4 +Example 2: Input: (1,2,3) Output: 0 +Example 3: Input: (1,1,1,1) Output: 6 + +=cut + +# IO NOTES: + +# NOTE: Input is via built-in array of arrays, or via @ARGV. +# If using @ARGV, input should be a space-separated list +# of integers. + +# NOTE: Output is to stdout and will be the input list followed +# by number of Good Pairs found. + +# PRELIMINARIES: + +use v5.36; +$" = ', '; + +# DEFAULT INPUT: +my @arrays = ([1,2,3,1,1,3],[1,2,3],[1,1,1,1]); + +# NON-DEFAULT INPUT: +if (@ARGV) {@arrays = ([@ARGV])} + +# MAIN BODY OF SCRIPT: +for (@arrays){ + my @array = @{$_}; + my @good; + for ( my $i = 0 ; $i <= $#array - 1 ; ++$i ){ + for ( my $j = $i + 1 ; $j <= $#array - 0 ; ++$j ){ + if ( $array[$i] == $array[$j] ){ + push @good, [$i, $j];}}} + my $good_count = scalar(@good); + say ''; + say "list = (@array)"; + say "Found $good_count \"Good Pairs\" of list indices:"; + say "(@{$_})" for @good}
\ No newline at end of file diff --git a/challenge-199/robbie-hatley/perl/ch-2.pl b/challenge-199/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..120acb8d0e --- /dev/null +++ b/challenge-199/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,69 @@ +#! /usr/bin/perl + +# /d/rhe/PWCC/199/ch-2 +# "Good Triplets" + +=pod + +Task 2: Good Triplets +Submitted by: Mohammad S Anwar + +You are given an array of integers, @array, and three integers +$x,$y,$z. Write a script to find out total Good Triplets in +the given array. A triplet array[i], array[j], array[k] is a +"Good Triplet" if it satisfies the following conditions: +a) 0 <= i < j < k < n (size of given array) +b) abs(array[i] - array[j]) <= x +c) abs(array[j] - array[k]) <= y +d) abs(array[k] - array[i]) <= z + +Example 1: +Input: @array = (3,0,1,1,9,7), $x = 7, $y = 2, $z = 3 +Output: 4 + +Example 2: +Input: @array = (1,1,2,2,3), $x = 0, $y = 0, $z = 1 +Output: 0 + +=cut + +# IO NOTES: + +# NOTE: Input is via built-in array of arrays, or via @ARGV. +# If using @ARGV, input should be a space-separated list +# of integers; the final 3 will be construed as being +# $x, $y, $z and the remainder as being @array. + +# NOTE: Output is to stdout and will be the input array and +# parameters followed by number of Good Triplets found. + +# PRELIMINARIES: + +use v5.36; +$" = ', '; + +# DEFAULT INPUT: +my @arrays = ([3,0,1,1,9,7,7,2,3],[1,1,2,2,3,0,0,1]); + +# NON-DEFAULT INPUT: +if (@ARGV) {@arrays = ([@ARGV])} + +# MAIN BODY OF SCRIPT: +for (@arrays){ + my @array = @{$_}; + my $z = pop @array; + my $y = pop @array; + my $x = pop @array; + my @good; + for ( my $i = 0 ; $i <= $#array - 2 ; ++$i ){ + for ( my $j = $i + 1 ; $j <= $#array - 1 ; ++$j ){ + for ( my $k = $j + 1 ; $k <= $#array - 0 ; ++$k ){ + if ( abs($array[$i] - $array[$j]) <= $x + && abs($array[$j] - $array[$k]) <= $y + && abs($array[$k] - $array[$i]) <= $z){ + push @good, [@array[$i, $j, $k]];}}}} + my $good_count = scalar(@good); + say ''; + say "array = (@array)"; + say "Found $good_count \"Good Triplets\" of array values:"; + say "(@{$_})" for @good}
\ No newline at end of file |
