diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-19 12:00:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-19 12:00:14 +0100 |
| commit | 40e5062d8ac52d6a0fef88f29fafbbdfca0138bf (patch) | |
| tree | fe030d6f3046a075e7b8c347dfe5a59f814b151c | |
| parent | c38c5f2b2af22ddcfa4d56ef81b8572dfa5c43f1 (diff) | |
| parent | 58475afb0d5e3d9654652e20b020f04d5c510c4e (diff) | |
| download | perlweeklychallenge-club-40e5062d8ac52d6a0fef88f29fafbbdfca0138bf.tar.gz perlweeklychallenge-club-40e5062d8ac52d6a0fef88f29fafbbdfca0138bf.tar.bz2 perlweeklychallenge-club-40e5062d8ac52d6a0fef88f29fafbbdfca0138bf.zip | |
Merge pull request #12544 from PerlBoy1967/branch-for-challenge-334
w334 - Task 1 & 2
| -rwxr-xr-x | challenge-334/perlboy1967/perl/ch1.pl | 33 | ||||
| -rwxr-xr-x | challenge-334/perlboy1967/perl/ch2.pl | 50 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-334/perlboy1967/perl/ch1.pl b/challenge-334/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..c2d7df67b9 --- /dev/null +++ b/challenge-334/perlboy1967/perl/ch1.pl @@ -0,0 +1,33 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-334#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Range Sum +Submitted by: Mohammad Sajid Anwar + +You are given a list integers and pair of indices.. + +Write a script to return the sum of integers between the given indices (inclusive). + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::Util qw(sum0); + +sub rangeSum ($il,$ih,@ints) { + return sum0(@ints[$il..$ih]); +} + +is(rangeSum(0,2,-2,0,3,-5,2,-1),1,'Example 1'); +is(rangeSum(1,3,1,-2,3,-4,5),-3,'Example 2'); +is(rangeSum(3,4,1,0,2,-1,3),2,'Example 3'); +is(rangeSum(0,3,-5,4,-3,2,-1,0),-2,'Example 4'); +is(rangeSum(0,2,-1,0,2,-3,-2,1),1,'Example 5'); + +done_testing; diff --git a/challenge-334/perlboy1967/perl/ch2.pl b/challenge-334/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..86a8dcbeb2 --- /dev/null +++ b/challenge-334/perlboy1967/perl/ch2.pl @@ -0,0 +1,50 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-334#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Nearest Valid Point +Submitted by: Mohammad Sajid Anwar + +You are given current location as two integers: x and y. You are also given +a list of points on the grid. + +A point is considered valid if it shares either the same x-coordinate or the +same y-coordinate as the current location. + +Write a script to return the index of the valid point that has the smallest +Manhattan distance to the current location. If multiple valid points are tied +for the smallest distance, return the one with the lowest index. If no valid +points exist, return -1. + +|| The Manhattan distance between two points (x1, y1) and (x2, y2) is calculated +|| as: |x1 - x2| + |y1 - y2| + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::Util qw(min); +use List::MoreUtils qw(firstidx); + +sub numberOfSmallestManhattanDistance ($x,$y,@points) { + my $i = 0; + my @p = map { [@$_, abs($x-$$_[0]) + abs($y-$$_[1])] } + grep { $$_[0] == $x or $$_[1] == $y } + map { [@$_, $i++] } @points; + return -1 unless @p; + my @min = min(map { $$_[3] } @p); + $p[firstidx { $$_[3] == $min[0] } @p][2]; +} + +is(numberOfSmallestManhattanDistance(3,4,[1,2],[3,1],[2,4],[2,3]),2,'Example 1'); +is(numberOfSmallestManhattanDistance(2,5,[3,4],[2,3],[1,5],[2,5]),3,'Example 2'); +is(numberOfSmallestManhattanDistance(1,1,[2,2],[3,3],[4,4]),-1,'Example 3'); +is(numberOfSmallestManhattanDistance(0,0,[0,1],[1,0],[0,2],[2,0]),0,'Example 4'); +is(numberOfSmallestManhattanDistance(5,5,[5,6],[6,5],[5,4],[4,5]),0,'Example 5'); + +done_testing; |
