diff options
| author | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-09-15 16:19:15 +0200 |
|---|---|---|
| committer | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-09-15 16:19:15 +0200 |
| commit | 355248e42a8b7e01ab8af0e466bcc440d6a0809e (patch) | |
| tree | 720b9cf1e980fef1b845947777f303ddf19ce392 | |
| parent | b2cc80b5507fc4f13b2eec8050f70ef12019ffb6 (diff) | |
| download | perlweeklychallenge-club-355248e42a8b7e01ab8af0e466bcc440d6a0809e.tar.gz perlweeklychallenge-club-355248e42a8b7e01ab8af0e466bcc440d6a0809e.tar.bz2 perlweeklychallenge-club-355248e42a8b7e01ab8af0e466bcc440d6a0809e.zip | |
Challenge 339
| -rw-r--r-- | challenge-339/mahnkong/perl/ch-1.pl | 15 | ||||
| -rw-r--r-- | challenge-339/mahnkong/perl/ch-2.pl | 22 |
2 files changed, 37 insertions, 0 deletions
diff --git a/challenge-339/mahnkong/perl/ch-1.pl b/challenge-339/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..ef3c568242 --- /dev/null +++ b/challenge-339/mahnkong/perl/ch-1.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + my @sorted = sort {$a <=> $b} map { abs($_) } @ints; + return $sorted[-2]*$sorted[-1] - $sorted[0]*$sorted[1]; +} + +is(run(5, 9, 3, 4, 6), 42, "Example 1"); +is(run(1, -2, 3, -4), 10, "Example 2"); +is(run(-3, -1, -2, -4), 10, "Example 3"); +is(run(10, 2, 0, 5, 1), 50, "Example 4"); +is(run(7, 8, 9, 10, 10), 44, "Example 5"); diff --git a/challenge-339/mahnkong/perl/ch-2.pl b/challenge-339/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..2d3a08faf2 --- /dev/null +++ b/challenge-339/mahnkong/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@gain) { + my $peak = 0; + my $current = 0; + + for (my $i = 0; $i <= $#gain; $i++) { + $current += $gain[$i]; + $peak = $current if $current > $peak; + } + + return $peak; +} + +is(run(-5, 1, 5, -9, 2), 1, "Example 1"); +is(run(10, 10, 10, -25), 30, "Example 2"); +is(run(3, -4, 2, 5, -6, 1), 6, "Example 2"); +is(run(-1, -2, -3, -4), 0, "Example 4"); +is(run(-10, 15, 5), 10, "Example 5"); |
