diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-13 17:23:01 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-13 17:23:01 +0000 |
| commit | 035cf02be5dd61dac43bb76e33db07067c92f018 (patch) | |
| tree | 16a742b0e9cb25b0ab4a34425c18e4c33354f498 /challenge-243 | |
| parent | 621356f5385f38edb288f5413f19386bafd8ad10 (diff) | |
| parent | b00ba3344b779c7565c0bd485c4577d65d874288 (diff) | |
| download | perlweeklychallenge-club-035cf02be5dd61dac43bb76e33db07067c92f018.tar.gz perlweeklychallenge-club-035cf02be5dd61dac43bb76e33db07067c92f018.tar.bz2 perlweeklychallenge-club-035cf02be5dd61dac43bb76e33db07067c92f018.zip | |
Merge pull request #9054 from PerlBoy1967/branch-for-challenge-243
w243 - Task 1 & 2
Diffstat (limited to 'challenge-243')
| -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; |
