aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-14 22:44:22 +0000
committerGitHub <noreply@github.com>2023-01-14 22:44:22 +0000
commit61817f6eaaa32307187648a3de429969c30bbd7f (patch)
treebeb0497bb6afe0c4160106605d7124808cbfc6a7
parentf9ad09de84d3b3f4516e80aadf14d590d37ed0fb (diff)
parentf782a2eb7d03154cc8242030b78f02332c980f1a (diff)
downloadperlweeklychallenge-club-61817f6eaaa32307187648a3de429969c30bbd7f.tar.gz
perlweeklychallenge-club-61817f6eaaa32307187648a3de429969c30bbd7f.tar.bz2
perlweeklychallenge-club-61817f6eaaa32307187648a3de429969c30bbd7f.zip
Merge pull request #7408 from Solathian/branch-for-challenge-199
adding files for challenge 199
-rw-r--r--challenge-199/solathian/perl/ch-1.pl27
-rw-r--r--challenge-199/solathian/perl/ch-2.pl37
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-199/solathian/perl/ch-1.pl b/challenge-199/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..fa730a6ebe
--- /dev/null
+++ b/challenge-199/solathian/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!usr/bin/perl
+use v5.36;
+
+# Challange 198 - 1 - Good Pairs
+# 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.
+
+gp(1,2,3,1,1,3); # 4
+gp(1,2,3); # 0
+gp(1,1,1,1); # 6
+
+sub gp(@array)
+{
+ my $count = 0;
+
+ for(my $i = 0; $i < $#array; $i++)
+ {
+ for(my $j = $i+1; $j < @array; $j++)
+ {
+ $count++ if($array[$i] == $array[$j]);
+ }
+ }
+
+ say $count;
+ return $count
+} \ No newline at end of file
diff --git a/challenge-199/solathian/perl/ch-2.pl b/challenge-199/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..3d377c551b
--- /dev/null
+++ b/challenge-199/solathian/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!usr/bin/perl
+
+use v5.36;
+use builtin 'indexed';
+no warnings builtin::indexed;
+
+# Challange 199 - 2 - 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:
+
+goodTriplets([3,0,1,1,9,7], 7, 2, 3); # 4
+goodTriplets([1,1,2,2,3], 0, 0, 1); # 0
+
+sub goodTriplets($array, $x, $y, $z )
+{
+ my $count = 0;
+
+ foreach my ($i, $iVal) (indexed @$array)
+ {
+ foreach my ($j, $jVal) (indexed @$array)
+ {
+ foreach my ($k, $kVal) (indexed @$array)
+ {
+ next if( not ( $i < $j < $k)); # a) 0 <= i < j < k <= n (size of given array)
+ next if( not (abs( $iVal - $jVal) <= $x)); # b) abs(array[i] - array[j]) <= x
+ next if( not (abs( $jVal - $kVal) <= $y)); # c) abs(array[j] - array[k]) <= y
+ next if( not (abs( $iVal - $kVal) <= $y)); # d) abs(array[i] - array[k]) <= z
+
+ $count++;
+ }
+ }
+ }
+
+ say $count;
+ return $count
+} \ No newline at end of file