aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2023-11-14 12:00:06 +0100
committerMariano Spadaccini <spadacciniweb@gmail.com>2023-11-14 12:00:06 +0100
commitc8427b145db3dd7832ea56ca49242001dd6ebfc0 (patch)
treef7703c968b425fbbdad7e0ea8d4db9b539c47f17 /challenge-243
parentd20e7296170b997b7e690a58b79156f6c81f1cd2 (diff)
downloadperlweeklychallenge-club-c8427b145db3dd7832ea56ca49242001dd6ebfc0.tar.gz
perlweeklychallenge-club-c8427b145db3dd7832ea56ca49242001dd6ebfc0.tar.bz2
perlweeklychallenge-club-c8427b145db3dd7832ea56ca49242001dd6ebfc0.zip
PWC-243 in Perl
Diffstat (limited to 'challenge-243')
-rw-r--r--challenge-243/spadacciniweb/perl/ch-1.pl34
-rw-r--r--challenge-243/spadacciniweb/perl/ch-2.pl38
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-243/spadacciniweb/perl/ch-1.pl b/challenge-243/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..8868d8f565
--- /dev/null
+++ b/challenge-243/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+# Task 1: Reverse Pairs
+# Submitted by: Mohammad S Anwar
+#
+# 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].
+#
+# Example 1
+# Input: @nums = (1, 3, 2, 3, 1)
+# Output: 2
+#
+# (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
+# (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1
+#
+# Example 2
+# Input: @nums = (2, 4, 3, 5, 1)
+# Output: 3
+#
+# (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
+# (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
+# (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1
+
+use strict;
+use warnings;
+use PDL;
+
+
+my $m_input = pdl(@ARGV);
+my $m = $m_input->dummy(1, scalar @ARGV);
+my $m_t = $m->transpose;
+printf "$m_input -> %d", (($m->xvals < $m->yvals) & ($m > 2*$m_t))->sum;
diff --git a/challenge-243/spadacciniweb/perl/ch-2.pl b/challenge-243/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..49f3d035e1
--- /dev/null
+++ b/challenge-243/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+# Task 2: Floor Sum
+# Submitted by: Mohammad S Anwar
+#
+# 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.
+#
+# Example 1
+# Input: @nums = (2, 5, 9)
+# Output: 10
+#
+# floor(2 / 5) = 0
+# floor(2 / 9) = 0
+# floor(5 / 9) = 0
+# floor(2 / 2) = 1
+# floor(5 / 5) = 1
+# floor(9 / 9) = 1
+# floor(5 / 2) = 2
+# floor(9 / 2) = 4
+# floor(9 / 5) = 1
+#
+# Example 2
+# Input: @nums = (7, 7, 7, 7, 7, 7, 7)
+# Output: 49
+
+use strict;
+use warnings;
+use PDL;
+
+die "Input error\n"
+ if scalar @ARGV < 1
+ or
+ (scalar map { $_ and $_ =~ /^\d+$/ ? () : 1 }
+ @ARGV) != 0;
+
+my $m = pdl(@ARGV);
+printf "$m -> %d\n", ($m/$m->dummy(0))->floor->sum;