aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-18 00:03:26 +0100
committerGitHub <noreply@github.com>2025-08-18 00:03:26 +0100
commit6366813d61f7b7ac8f0fb7a0bf5f384aa74a2d61 (patch)
treef035d5483b2645dad92c60699c6b3fb028c89958
parent36cc6a3f6b5b5d7f3e7c072a251435352c998dd2 (diff)
parent22c845a9d00a86a3ed9a3d76b54a5a3daa92db3c (diff)
downloadperlweeklychallenge-club-6366813d61f7b7ac8f0fb7a0bf5f384aa74a2d61.tar.gz
perlweeklychallenge-club-6366813d61f7b7ac8f0fb7a0bf5f384aa74a2d61.tar.bz2
perlweeklychallenge-club-6366813d61f7b7ac8f0fb7a0bf5f384aa74a2d61.zip
Merge pull request #12527 from wanderdoc/master
PWC 334 (wanderdoc)
-rw-r--r--challenge-334/wanderdoc/perl/ch-1.pl76
-rw-r--r--challenge-334/wanderdoc/perl/ch-2.pl118
2 files changed, 194 insertions, 0 deletions
diff --git a/challenge-334/wanderdoc/perl/ch-1.pl b/challenge-334/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..c7ced5d4f8
--- /dev/null
+++ b/challenge-334/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a list integers and pair of indices..
+Write a script to return the sum of integers between the given indices (inclusive).
+
+Example 1
+
+Input: @ints = (-2, 0, 3, -5, 2, -1), $x = 0, $y = 2
+Output: 1
+
+Elements between indices (0, 2) => (-2, 0, 3)
+Range Sum: (-2) + 0 + 3 => 1
+
+
+Example 2
+
+Input: @ints = (1, -2, 3, -4, 5), $x = 1, $y = 3
+Output: -3
+
+Elements between indices (1, 3) => (-2, 3, -4)
+Range Sum: (-2) + 3 + (-4) => -3
+
+
+Example 3
+
+Input: @ints = (1, 0, 2, -1, 3), $x = 3, $y = 4
+Output: 2
+
+Elements between indices (3, 4) => (-1, 3)
+Range Sum: (-1) + 3 => 2
+
+
+Example 4
+
+Input: @ints = (-5, 4, -3, 2, -1, 0), $x = 0, $y = 3
+Output: -2
+
+Elements between indices (0, 3) => (-5, 4, -3, 2)
+Range Sum: (-5) + 4 + (-3) + 2 => -2
+
+
+Example 5
+
+Input: @ints = (-1, 0, 2, -3, -2, 1), $x = 0, $y = 2
+Output: 1
+
+Elements between indices (0, 2) => (-1, 0, 2)
+Range Sum: (-1) + 0 + 2 => 1
+=cut
+
+
+use List::Util qw(sum);
+use Test2::V0 -no_srand => 1;
+
+
+is(range_sum([-2, 0, 3, -5, 2, -1], 0, 2), 1, 'Example 1');
+is(range_sum([1, -2, 3, -4, 5], 1, 3), -3, 'Example 2');
+is(range_sum([1, 0, 2, -1, 3], 3, 4), 2, 'Example 3');
+is(range_sum([-5, 4, -3, 2, -1, 0], 0, 3), -2, 'Example 4');
+is(range_sum([-1, 0, 2, -3, -2, 1], 0, 2), 1, 'Example 5');
+done_testing();
+
+sub range_sum
+{
+ my ($aref, $x, $y) = @_;
+ die "Index beyond array limits!"
+ if ($x > $#$aref or $y > $#$aref or $x < 0 or $y < 0);
+ if ($x > $y)
+ {
+ ($x, $y) = ($y, $x);
+ }
+ return sum(@$aref[$x..$y]);
+}
diff --git a/challenge-334/wanderdoc/perl/ch-2.pl b/challenge-334/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..ff8d65b9c9
--- /dev/null
+++ b/challenge-334/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,118 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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|
+
+
+Example 1
+
+Input: $x = 3, $y = 4, @points ([1, 2], [3, 1], [2, 4], [2, 3])
+Output: 2
+
+Valid points: [3, 1] (same x), [2, 4] (same y)
+
+Manhattan distances:
+ [3, 1] => |3-3| + |4-1| = 3
+ [2, 4] => |3-2| + |4-4| = 1
+
+Closest valid point is [2, 4] at index 2.
+
+
+Example 2
+
+Input: $x = 2, $y = 5, @points ([3, 4], [2, 3], [1, 5], [2, 5])
+Output: 3
+
+Valid points: [2, 3], [1, 5], [2, 5]
+
+Manhattan distances:
+ [2, 3] => 2
+ [1, 5] => 1
+ [2, 5] => 0
+
+Closest valid point is [2, 5] at index 3.
+
+
+Example 3
+
+Input: $x = 1, $y = 1, @points ([2, 2], [3, 3], [4, 4])
+Output: -1
+
+No point shares x or y with (1, 1).
+
+
+Example 4
+
+Input: $x = 0, $y = 0, @points ([0, 1], [1, 0], [0, 2], [2, 0])
+Output: 0
+
+Valid points: all of them
+
+Manhattan distances:
+ [0, 1] => 1
+ [1, 0] => 1
+ [0, 2] => 2
+ [2, 0] => 2
+
+Tie between index 0 and 1, pick the smaller index: 0
+
+
+Example 5
+
+Input: $x = 5, $y = 5, @points ([5, 6], [6, 5], [5, 4], [4, 5])
+Output: 0
+
+Valid points: all of them
+ [5, 6] => 1
+ [6, 5] => 1
+ [5, 4] => 1
+ [4, 5] => 1
+
+All tie, return the one with the lowest index: 0
+=cut
+
+
+
+
+
+
+use Test2::V0 -no_srand => 1;
+is(nearest_valid_point(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]), 2, 'Example 1');
+is(nearest_valid_point(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]), 3, 'Example 2');
+is(nearest_valid_point(1, 1, [[2, 2], [3, 3], [4, 4]]), -1, 'Example 3');
+is(nearest_valid_point(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]), 0, 'Example 4');
+is(nearest_valid_point(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]), 0, 'Example 5');
+done_testing();
+
+sub nearest_valid_point
+{
+ my ($x, $y, $grid) = @_;
+ my @nearest =
+ sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] }
+ map {[manhattan_distance($grid->[$_][0], $grid->[$_][1], $x, $y), $_] }
+ grep { is_valid_point($grid->[$_][0], $grid->[$_][1], $x, $y) }
+ 0 .. $#$grid;
+ return scalar @nearest ? $nearest[0][1] : -1;
+}
+
+
+sub is_valid_point
+{
+ my ($x1, $y1, $x2, $y2) = @_;
+ return (($x1 == $x2) or ($y1 == $y2));
+}
+
+
+sub manhattan_distance
+{
+ my ($x1, $y1, $x2, $y2) = @_;
+ return abs($x1 - $x2) + abs($y1 - $y2);
+}