aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-12 13:23:35 +0100
committerGitHub <noreply@github.com>2025-08-12 13:23:35 +0100
commit93759a600dd661a59eebe6b221bb2f7aa82700ff (patch)
treedc5ba532097ece2171d408056e2cac6ab6543cff
parent99e1ae03fb222df9173bd37c2db4e94e080a5fbb (diff)
parent9cae8ffa03003a572ab1e0aa6db78e403e7b4ead (diff)
downloadperlweeklychallenge-club-93759a600dd661a59eebe6b221bb2f7aa82700ff.tar.gz
perlweeklychallenge-club-93759a600dd661a59eebe6b221bb2f7aa82700ff.tar.bz2
perlweeklychallenge-club-93759a600dd661a59eebe6b221bb2f7aa82700ff.zip
Merge pull request #12504 from zapwai/branch-for-334
Week 334
-rw-r--r--challenge-334/zapwai/perl/ch-1.pl23
-rw-r--r--challenge-334/zapwai/perl/ch-2.pl42
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-334/zapwai/perl/ch-1.pl b/challenge-334/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..c1922706da
--- /dev/null
+++ b/challenge-334/zapwai/perl/ch-1.pl
@@ -0,0 +1,23 @@
+use v5.38;
+
+sub proc($x, $y, @int) {
+ say "Input: @int\t \$x = $x\t \$y = $y";
+ my $sum = 0;
+ for my $i ($x .. $y) {
+ $sum += $int[$i];
+ }
+ say "Output: $sum";
+}
+
+my @ints = (-2, 0, 3, -5, 2, -1);
+my $x = 0;
+my $y = 2;
+proc($x, $y, @ints);
+@ints = (1, -2, 3, -4, 5); $x = 1; $y = 3;
+proc($x, $y, @ints);
+@ints = (1, 0, 2, -1, 3); $x = 3; $y = 4;
+proc($x, $y, @ints);
+@ints = (-5, 4, -3, 2, -1, 0); $x = 0; $y = 3;
+proc($x, $y, @ints);
+@ints = (-1, 0, 2, -3, -2, 1); $x = 0; $y = 2;
+proc($x, $y, @ints);
diff --git a/challenge-334/zapwai/perl/ch-2.pl b/challenge-334/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..ddd70e7576
--- /dev/null
+++ b/challenge-334/zapwai/perl/ch-2.pl
@@ -0,0 +1,42 @@
+use v5.38;
+sub proc($x, $y, @p) {
+ print "Input: \$x = $x, \$y = $y, \@points = ";
+ print "[".join(", ", @$_)."] " for (@p);
+ print "\n";
+ my @ind; # list of valid points
+ my @dist; # list of distances
+ for my $i (0 .. $#p) {
+ my @pt = @{$p[$i]};
+ my $current_x = $pt[0];
+ my $current_y = $pt[1];
+ if ($current_x == $x || $current_y == $y) {
+ push @ind, $i;
+ push @dist, abs($x - $current_x) + abs($y - $current_y);
+ }
+ }
+ if (@ind) {
+ my $min = $dist[0];
+ my $minind = 0;
+ for my $i (0 .. $#ind) {
+ if ($min > $dist[$i]) {
+ $min = $dist[$i];
+ $minind = $i;
+ }
+ }
+ my $o = $ind[$minind];
+ say "Output: $o";
+ } else {
+ say "Output: -1";
+ }
+}
+my ($x, $y, @points);
+$x = 3, $y = 4, @points = ([1, 2], [3, 1], [2, 4], [2, 3]);
+proc($x, $y, @points);
+$x = 2, $y = 5, @points = ([3, 4], [2, 3], [1, 5], [2, 5]);
+proc($x, $y, @points);
+$x = 1, $y = 1, @points = ([2, 2], [3, 3], [4, 4]);
+proc($x, $y, @points);
+$x = 0, $y = 0, @points = ([0, 1], [1, 0], [0, 2], [2, 0]);
+proc($x, $y, @points);
+$x = 5, $y = 5, @points = ([5, 6], [6, 5], [5, 4], [4, 5]);
+proc($x, $y, @points);