aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-243')
-rwxr-xr-xchallenge-243/wanderdoc/perl/ch-1.pl43
-rwxr-xr-xchallenge-243/wanderdoc/perl/ch-2.pl44
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-243/wanderdoc/perl/ch-1.pl b/challenge-243/wanderdoc/perl/ch-1.pl
new file mode 100755
index 0000000000..168c53e9e2
--- /dev/null
+++ b/challenge-243/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+=cut
+use Test2::V0;
+
+sub reverse_pairs
+{
+ my @arr = @_;
+ my @output;
+ for my $i ( 0 .. $#arr -1 )
+ {
+ for my $j ( $i + 1 .. $#arr )
+ {
+ if ( $arr[$i] > 2 * $arr[$j] )
+ {
+ push @output, [$i, $j];
+ }
+ }
+ }
+ return @output;
+}
+
+
+is([reverse_pairs(1, 3, 2, 3, 1)], [[1,4],[3,4]], 'Example 1');
+is([reverse_pairs(2, 4, 3, 5, 1)], [[1,4],[2,4],[3,4]], 'Example 2');
+done_testing(); \ No newline at end of file
diff --git a/challenge-243/wanderdoc/perl/ch-2.pl b/challenge-243/wanderdoc/perl/ch-2.pl
new file mode 100755
index 0000000000..cf978d4b0a
--- /dev/null
+++ b/challenge-243/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+
+=cut
+
+use Test2::V0;
+
+sub floor_sum
+{
+ my @arr = @_;
+ my $sum = 0;
+ for my $i ( 0 .. $#arr )
+ {
+ for my $j ( 0 .. $#arr )
+ {
+ $sum += int($arr[$i]/$arr[$j]);
+ }
+ }
+ return $sum;
+}
+
+
+is(floor_sum(2, 5, 9), 10, 'Example 1');
+is(floor_sum(7, 7, 7, 7, 7, 7, 7), 49, 'Example 2');
+done_testing(); \ No newline at end of file