diff options
| author | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-08-14 11:16:35 +0200 |
|---|---|---|
| committer | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-08-14 11:16:35 +0200 |
| commit | c5d20ffd592ad9471120cd9ca243aa34ef02421e (patch) | |
| tree | 66fd4bdee1eece9fa8c66eca51bb17c5f6f96865 | |
| parent | 3f0c25ac7a8089ff3d921c6e4926ff88ebd806cd (diff) | |
| download | perlweeklychallenge-club-c5d20ffd592ad9471120cd9ca243aa34ef02421e.tar.gz perlweeklychallenge-club-c5d20ffd592ad9471120cd9ca243aa34ef02421e.tar.bz2 perlweeklychallenge-club-c5d20ffd592ad9471120cd9ca243aa34ef02421e.zip | |
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"); + |
