diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-22 18:25:12 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-26 14:47:13 +0200 |
| commit | 4e200a98872349e19f603ec248144e95c6968541 (patch) | |
| tree | b8d5469abb5285436d20d686035e2e35fffd8fc8 | |
| parent | 430f300fd7abb93023633e53f7d3d0768d0a8723 (diff) | |
| download | perlweeklychallenge-club-4e200a98872349e19f603ec248144e95c6968541.tar.gz perlweeklychallenge-club-4e200a98872349e19f603ec248144e95c6968541.tar.bz2 perlweeklychallenge-club-4e200a98872349e19f603ec248144e95c6968541.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-218/jo-37/perl/ch-1.pl | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/challenge-218/jo-37/perl/ch-1.pl b/challenge-218/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..acde924b6e --- /dev/null +++ b/challenge-218/jo-37/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV >= 3; +usage: $0 [-examples] [-tests] [--] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of at least three numbers + +EOS + + +### Input and Output + +say max_prod(@ARGV); + + +### Implementation + +# There are only two candidates for the maximum product of three +# elements: +# - the product of the three largest elements +# - the product of the two smallest elements with the largest +sub max_prod { + # For the sake of convenience, sort the elements. + my @s = sort {$a <=> $b} @_; + my $left = $s[0] * $s[1] * $s[-1]; + my $right = $s[-3] * $s[-2] * $s[-1]; + + $left > $right ? $left : $right; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is max_prod(3, 1, 2), 6, 'example 1'; + is max_prod(4, 1, 3, 2), 24, 'example 2'; + is max_prod(-1, 0, 1, 3, 1), 3, 'example 3'; + is max_prod(-8, 2, -9, 0, -4, 3), 216, 'example 4'; + } + + SKIP: { + skip "tests" unless $tests; + + is max_prod(-1, -2, -3, -4, -5), -6, 'all negative'; + is max_prod(1, 2, 3, 4, -5), 24, 'one negative'; + is max_prod(1, 2, -3, -4, 4, 5), 60, 'left wins'; + is max_prod(1, -2, 3, -4, 4, 5), 60, 'right wins'; + } + + done_testing; + exit; +} |
