aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-19 21:54:56 +0000
committerGitHub <noreply@github.com>2023-11-19 21:54:56 +0000
commit09ed97284bea0d122fb6f5e57a3b6520e825a206 (patch)
treecdce1d5b568b77432e9e24d4f5d1c56b51c2f8d9
parent5e1177d6fa5a7e7ecb3e2b1d7327163e687be4d4 (diff)
parent54f5e9b0b7956c918358d7113480049d85724ec1 (diff)
downloadperlweeklychallenge-club-09ed97284bea0d122fb6f5e57a3b6520e825a206.tar.gz
perlweeklychallenge-club-09ed97284bea0d122fb6f5e57a3b6520e825a206.tar.bz2
perlweeklychallenge-club-09ed97284bea0d122fb6f5e57a3b6520e825a206.zip
Merge pull request #9091 from Solathian/branch-for-challenge-243
Added files for 243
-rw-r--r--challenge-243/solathian/perl/ch-1.pl30
-rw-r--r--challenge-243/solathian/perl/ch-2.pl28
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-243/solathian/perl/ch-1.pl b/challenge-243/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..e577f69bd8
--- /dev/null
+++ b/challenge-243/solathian/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!usr/bin/perl
+use v5.38;
+
+use builtin "indexed";
+no warnings "experimental";
+
+
+# Challenge 243 - 1 - Reverse Pairs
+# 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) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j].
+
+revPair((1, 3, 2, 3, 1));
+revPair((2, 4, 3, 5, 1));
+
+sub revPair(@arr)
+{
+ my @pairs;
+
+ foreach my ($i, $iVal) (indexed @arr)
+ {
+ foreach my ($j, $jVal) (indexed @arr)
+ {
+ # just going with the debug approach, a simple counter would also suffice
+ push(@pairs, [$i, $j]) if( $i < $j and $iVal > 2*$jVal);
+ }
+ }
+
+ say "Number of pairs: ", scalar @pairs;
+} \ No newline at end of file
diff --git a/challenge-243/solathian/perl/ch-2.pl b/challenge-243/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..90b8d13b56
--- /dev/null
+++ b/challenge-243/solathian/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!usr/bin/perl
+use v5.38;
+use builtin "floor";
+no warnings "experimental";
+
+# Challenge 243 - 2 - Floor Sum
+# 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.
+
+
+floorSum(2, 5, 9);
+floorSum(7, 7, 7, 7, 7, 7, 7);
+
+sub floorSum(@list)
+{
+ my $sum;
+
+ foreach my ($iVal) (@list)
+ {
+ foreach my ($jVal) (@list)
+ {
+ $sum += floor($iVal / $jVal)
+ }
+ }
+
+ say $sum;
+} \ No newline at end of file