diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-15 11:06:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-15 11:06:46 +0100 |
| commit | 91fc1e2a3d45918371c6aef884d0285e67ee0e16 (patch) | |
| tree | 98c234e97e28fa5315d79c1fd648735679eb8ca5 | |
| parent | 9ec12b6be9ab811c70b1a5e7a438b53934b39177 (diff) | |
| parent | c5d20ffd592ad9471120cd9ca243aa34ef02421e (diff) | |
| download | perlweeklychallenge-club-91fc1e2a3d45918371c6aef884d0285e67ee0e16.tar.gz perlweeklychallenge-club-91fc1e2a3d45918371c6aef884d0285e67ee0e16.tar.bz2 perlweeklychallenge-club-91fc1e2a3d45918371c6aef884d0285e67ee0e16.zip | |
Merge pull request #12518 from mahnkong/challenge-334
Challenge 334
| -rw-r--r-- | challenge-334/mahnkong/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-334/mahnkong/perl/ch-2.pl | 29 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-334/mahnkong/perl/ch-1.pl b/challenge-334/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..7867bd14db --- /dev/null +++ b/challenge-334/mahnkong/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($ints, $x, $y) { + my $sum = 0; + foreach my $i (@$ints[$x..$y]) { + $sum += $i; + } + return $sum; +} + +is(run([-2, 0, 3, -5, 2, -1], 0, 2), 1, "Example 1"); +is(run([1, -2, 3, -4, 5], 1, 3), -3, "Example 2"); +is(run([1, 0, 2, -1, 3], 3, 4), 2, "Example 3"); +is(run([-5, 4, -3, 2, -1, 0], 0, 3), -2, "Example 4"); +is(run([-1, 0, 2, -3, -2, 1], 0, 2), 1, "Example 5"); diff --git a/challenge-334/mahnkong/perl/ch-2.pl b/challenge-334/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..98413b2a4d --- /dev/null +++ b/challenge-334/mahnkong/perl/ch-2.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($x, $y, $points) { + my $smallest_distance; + my $result = -1; + + for (my $i = 0; $i < scalar(@$points); $i++) { + if ($x == $points->[$i]->[0] || $y == $points->[$i]->[1]) { + my $distance = abs($x - $points->[$i]->[0]) + abs($y - $points->[$i]->[1]); + + if (! defined $smallest_distance || $smallest_distance > $distance) { + $smallest_distance = $distance; + $result = $i; + } + } + } + + return $result; +} + +is(run(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]), 2, "Example 1"); +is(run(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]), 3, "Example 2"); +is(run(1, 1, [[2, 2], [3, 3], [4, 4]]), -1, "Example 3"); +is(run(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]), 0, "Example 4"); +is(run(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]), 0, "Example 5"); + |
