diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-08 17:56:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-08 17:56:08 +0100 |
| commit | 82572628274377f04b8dab41a2387f18f26eb504 (patch) | |
| tree | d9496712136d69749530ff94aa9ae2d2a6c5e0ff | |
| parent | 44a60d838c321b35d403b8dd6bc1231e96ba7f15 (diff) | |
| parent | bd07161ddc27463cc143890ca64e7f15db1ad402 (diff) | |
| download | perlweeklychallenge-club-82572628274377f04b8dab41a2387f18f26eb504.tar.gz perlweeklychallenge-club-82572628274377f04b8dab41a2387f18f26eb504.tar.bz2 perlweeklychallenge-club-82572628274377f04b8dab41a2387f18f26eb504.zip | |
Merge pull request #12646 from mahnkong/challenge-338
Challenge 338
| -rw-r--r-- | challenge-338/mahnkong/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-338/mahnkong/perl/ch-2.pl | 20 |
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-338/mahnkong/perl/ch-1.pl b/challenge-338/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..fa0877befe --- /dev/null +++ b/challenge-338/mahnkong/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@matrix) { + my $highest_sum; + foreach my $row (@matrix) { + my $sum = 0; + map { $sum += $_ } @$row; + $highest_sum = $sum if !defined $highest_sum || $sum > $highest_sum; + } + return $highest_sum; +} + +is(run([4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]), 16, "Example 1"); +is(run([1, 5], [7, 3], [3, 5]), 10, "Example 2"); +is(run([1, 2, 3], [3, 2, 1]), 6, "Example 3"); +is(run([2, 8, 7], [7, 1, 3], [1, 9, 5]), 17, "Example 4"); +is(run([10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]), 100, "Example 5"); diff --git a/challenge-338/mahnkong/perl/ch-2.pl b/challenge-338/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..fc5003fa94 --- /dev/null +++ b/challenge-338/mahnkong/perl/ch-2.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($arr1, $arr2) { + my @arr1 = sort {$a <=> $b} @$arr1; + my @arr2 = sort {$a <=> $b} @$arr2; + + my $d1 = abs($arr1[0] - $arr2[-1]); + my $d2 = abs($arr2[0] - $arr1[-1]); + + return $d1 > $d2 ? $d1 : $d2; +} + +is(run([4, 5, 7], [9, 1, 3, 4]), 6, "Example 1"); +is(run([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]), 6, "Example 2"); +is(run([2, 1, 11, 3], [2, 5, 10, 2]), 9, "Example 3"); +is(run([1, 2, 3], [3, 2, 1]), 2, "Example 4"); +is(run([1, 0, 2, 3], [5, 0]), 5, "Example 5"); |
