aboutsummaryrefslogtreecommitdiff
path: root/challenge-218/jo-37/perl/ch-1.pl
blob: acde924b6e9a12b826ba7e980788799d84ea7066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
}