diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-15 21:14:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-15 21:14:33 +0000 |
| commit | 5f0b8e5d6945116ef35febfbdcbef4f55a0a5965 (patch) | |
| tree | a938cb1255c9c9ffc5b71fc43b0688d74a48e150 | |
| parent | e91f001227bba6a0883e8d3dc9c04ea0afe7351a (diff) | |
| parent | 77dfa22a7864fb9a05573afca63423fe5a8ba026 (diff) | |
| download | perlweeklychallenge-club-5f0b8e5d6945116ef35febfbdcbef4f55a0a5965.tar.gz perlweeklychallenge-club-5f0b8e5d6945116ef35febfbdcbef4f55a0a5965.tar.bz2 perlweeklychallenge-club-5f0b8e5d6945116ef35febfbdcbef4f55a0a5965.zip | |
Merge pull request #9071 from pme/challenge-243
challenge-243
| -rwxr-xr-x | challenge-243/peter-meszaros/perl/ch-1.pl | 57 | ||||
| -rwxr-xr-x | challenge-243/peter-meszaros/perl/ch-2.pl | 57 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-243/peter-meszaros/perl/ch-1.pl b/challenge-243/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..9c0468a0ed --- /dev/null +++ b/challenge-243/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# +# 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 Test::More; +use Data::Dumper; + +my $cases = [ + [1, 3, 2, 3, 1], + [2, 4, 3, 5, 1], +]; + +sub reverse_pairs +{ + my $l = shift; + + my $cnt = 0; + for my $i (0..($#$l-1)) { + for my $j (($i+1)..$#$l) { + ++$cnt if $l->[$i] > (2 * $l->[$j]); + } + } + return $cnt; +} + +is(reverse_pairs($cases->[0]), 2, '[1, 3, 2, 3, 1]'); +is(reverse_pairs($cases->[1]), 3, '[2, 4, 3, 5, 1]'); + +done_testing(); + +exit 0; + + diff --git a/challenge-243/peter-meszaros/perl/ch-2.pl b/challenge-243/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..7f89a8db31 --- /dev/null +++ b/challenge-243/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# +# 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 Test::More; +use Data::Dumper; + +my $cases = [ + [2, 5, 9], + [7, 7, 7, 7, 7, 7, 7], +]; + +sub floor_sum +{ + my $l = shift; + + my $sum = 0; + for my $i (@$l) { + for my $j (@$l) { + $sum += int($i / $j); + } + } + + return $sum; +} + +is(floor_sum($cases->[0]), 10, '[2, 5, 9]'); +is(floor_sum($cases->[1]), 49, '[7, 7, 7, 7, 7, 7, 7]'); +done_testing(); + +exit 0; |
