diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-15 21:43:10 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-15 21:43:10 +0000 |
| commit | f8199bdb99771c1f524b7b1e5e3abe8f0ae0ebc7 (patch) | |
| tree | d4ab037651de7ddf36042447d1c7ea0442f59290 | |
| parent | 4a4950187cdaa022b2f111637b7562b3948fac21 (diff) | |
| parent | 56084b92bf598822ffd73fb09bee9a3dbbda8692 (diff) | |
| download | perlweeklychallenge-club-f8199bdb99771c1f524b7b1e5e3abe8f0ae0ebc7.tar.gz perlweeklychallenge-club-f8199bdb99771c1f524b7b1e5e3abe8f0ae0ebc7.tar.bz2 perlweeklychallenge-club-f8199bdb99771c1f524b7b1e5e3abe8f0ae0ebc7.zip | |
Merge pull request #7411 from pip/branch-for-challenge-199
Pip Stuart's submission for challenge-199;
| -rw-r--r-- | challenge-199/pip/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-199/pip/perl/ch-2.pl | 34 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-199/pip/perl/ch-1.pl b/challenge-199/pip/perl/ch-1.pl new file mode 100644 index 0000000000..cc5d56dfb4 --- /dev/null +++ b/challenge-199/pip/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl Weekly Challenge #1 of Week #199 - Pip Stuart +# Good Pairs: 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. +# Example1: +# In-put: @list = (1,2,3,1,1,3) +# Output: 4 +# Example2: +# In-put: @list = (1,2,3 ) +# Output: 0 +# Example3: +# In-put: @list = (1,1,1,1 ) +# Output: 6 +use strict;use warnings;use utf8;use v5.10;my $d8VS='N19LHJac'; +sub GdPr {my @list=@_;my @parz=(); + print '(' . sprintf("%-11s",join(',',@list)) . ') => '; + for(0..$#list-1){ + for my $j ($_+1..$#list){ + if($list[$_] == $list[$j]){push(@parz,[$_,$j]);}} + } say scalar(@parz) . ';'; + return (@parz); +} +if(@ARGV){ + GdPr(@ARGV); +}else{ + GdPr(1,2,3,1,1,3); # => 4; + GdPr(1,2,3 ); # => 0; + GdPr(1,1,1,1 ); # => 6; +} diff --git a/challenge-199/pip/perl/ch-2.pl b/challenge-199/pip/perl/ch-2.pl new file mode 100644 index 0000000000..f6d542830f --- /dev/null +++ b/challenge-199/pip/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl Weekly Challenge #2 of Week #199 - Pip Stuart +# Good Triplets: 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: +# 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[i] - array[k]) <= z +# Example1: +# In-put: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +# Output: 4 +# Example2: +# In-put: @array = (1,1,2,2,3 ) and $x = 0, $y = 0, $z = 1 +# Output: 0 +# Note: I'm taking the liberty to accept x, y, z ahead of array. +use strict;use warnings;use utf8;use v5.10;my $d8VS='N19LHOLD'; +sub GdTr {my $x=shift;my $y=shift;my $z=shift;my @aray=@_;my @trpz=(); + print sprintf("x=%d y=%d z=%d a=%-11s",$x,$y,$z,join(',',@aray)) . ' => '; + for ( 0..$#aray-2){ + for my $j ($_+1..$#aray-1){ + for my $k ($j+1..$#aray ){ + if(abs($aray[$_]-$aray[$j]) <= $x && + abs($aray[$j]-$aray[$k]) <= $y && + abs($aray[$_]-$aray[$k]) <= $z){push(@trpz,[$_,$j,$k]);}}}} + say scalar(@trpz) . ';'; + return (@trpz); +} +if(@ARGV){ + GdTr(@ARGV); +}else{ #xyz + GdTr(7,2,3, 3,0,1,1,9,7); # => 4; + GdTr(0,0,1, 1,1,2,2,3 ); # => 0; +} |
