diff options
| -rwxr-xr-x | challenge-243/perlboy1967/perl/ch1.pl | 41 | ||||
| -rwxr-xr-x | challenge-243/perlboy1967/perl/ch2.pl | 36 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-243/perlboy1967/perl/ch1.pl b/challenge-243/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..b0d3bf22dc --- /dev/null +++ b/challenge-243/perlboy1967/perl/ch1.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 243 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-243 + +Author: Niels 'PerlBoy' van Dijke + +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]. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +sub nReversePairs (@) { + my $n = 0; + + for my $i (0 .. $#_ - 1) { + for my $j ($i + 1 .. $#_) { + $n++ if ($_[$i] > 2 * $_[$j]); + } + } + + return $n; +} + +is(nReversePairs(1,3,2,3,1),2); +is(nReversePairs(2,4,3,5,1),3); + +done_testing; diff --git a/challenge-243/perlboy1967/perl/ch2.pl b/challenge-243/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..78b89dbddc --- /dev/null +++ b/challenge-243/perlboy1967/perl/ch2.pl @@ -0,0 +1,36 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 243 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-243 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use Algorithm::Combinatorics qw(variations_with_repetition); +use POSIX qw(floor); +use List::Util qw(sum0); + +sub floorSum (@) { + sum0 map { floor($$_[0] / $$_[1]) } variations_with_repetition([@_],2); +} + +is(floorSum(2,5,9),10); +is(floorSum((7) x 7),49); + +done_testing; |
