diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-15 09:19:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-15 09:19:33 +0100 |
| commit | 2ea89c6063d10089472f1ab75c0c3c3c52176f28 (patch) | |
| tree | 4c9cb22f178fdd71fcb4ecd83be1475f5dfa79bf | |
| parent | 1ba03f888e4624b323d5fd39fb7fcd8aec9d71a5 (diff) | |
| parent | 96d465db8f4829f519fcf83f453553653639823a (diff) | |
| download | perlweeklychallenge-club-2ea89c6063d10089472f1ab75c0c3c3c52176f28.tar.gz perlweeklychallenge-club-2ea89c6063d10089472f1ab75c0c3c3c52176f28.tar.bz2 perlweeklychallenge-club-2ea89c6063d10089472f1ab75c0c3c3c52176f28.zip | |
Merge pull request #10835 from pme/challenge-199
challenge-199
| -rwxr-xr-x | challenge-199/peter-meszaros/perl/ch-1.pl | 74 | ||||
| -rwxr-xr-x | challenge-199/peter-meszaros/perl/ch-2.pl | 70 |
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-199/peter-meszaros/perl/ch-1.pl b/challenge-199/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..72a78d8925 --- /dev/null +++ b/challenge-199/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @list = (1,2,3,1,1,3) + Output: 4 + + There are 4 good pairs found as below: + (0,3) + (0,4) + (3,4) + (2,5) + +=head2 Example 2 + + Input: @list = (1,2,3) + Output: 0 + +=head2 Example 3 + + Input: @list = (1,1,1,1) + Output: 6 + + Good pairs are below: + (0,1) + (0,2) + (0,3) + (1,2) + (1,3) + (2,3) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 2, 3, 1, 1, 3], 4, 'Example 1'], + [[1, 2, 3], 0, 'Example 2'], + [[1, 1, 1, 1], 6, 'Example 3'], +]; + +sub good_pairs +{ + my $l = shift; + + my $cnt = 0; + for my $i (0 .. $#$l) { + for my $j ($i+1 .. $#$l) { + ++$cnt if $l->[$i] == $l->[$j]; + } + } + + return $cnt; +} + +for (@$cases) { + is(good_pairs($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-199/peter-meszaros/perl/ch-2.pl b/challenge-199/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..4600ded3cc --- /dev/null +++ b/challenge-199/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +# +=head1 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 good if it satisfies the following +conditions: + + b) abs(array[i] - array[j]) <= x + c) abs(array[j] - array[k]) <= y + d) abs(array[i] - array[k]) <= z + +=head2 Example 1 + + Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 + Output: 4 + + Good Triplets are as below: + (3,0,1) where (i=0, j=1, k=2) + (3,0,1) where (i=0, j=1, k=3) + (3,1,1) where (i=0, j=2, k=3) + (0,1,1) where (i=1, j=2, k=3) + +=head2 Example 2 + + Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 + Output: 0 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[3, 0, 1, 1, 9, 7], [7, 2, 3]], 4, 'Example 1'], + [[[1, 1, 2, 2, 3], [0, 0, 1]], 0, 'Example 2'], +]; + +sub good_triplets +{ + my $l = $_->[0]->[0]; + my ($x, $y, $z) = $_->[0]->[1]->@*; + + my $cnt = 0; + for my $i (0 .. $#$l) { + for my $j ($i+1 .. $#$l) { + for my $k ($j+1 .. $#$l) { + ++$cnt if abs($l->[$i] - $l->[$j]) <= $x && + abs($l->[$j] - $l->[$k]) <= $y && + abs($l->[$i] - $l->[$k]) <= $z; + } + } + } + + return $cnt; +} + +for (@$cases) { + is(good_triplets($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
