diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2023-05-23 16:28:19 +0100 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2023-05-23 16:28:19 +0100 |
| commit | 39bea06e4f5dcda47c08b89f12df6a24f81233bf (patch) | |
| tree | bf1dc829b274c9f5e2ae42a2a58a6bc02ba31538 /challenge-218 | |
| parent | c8670df894db150b6176b9128224122acf9a4a52 (diff) | |
| download | perlweeklychallenge-club-39bea06e4f5dcda47c08b89f12df6a24f81233bf.tar.gz perlweeklychallenge-club-39bea06e4f5dcda47c08b89f12df6a24f81233bf.tar.bz2 perlweeklychallenge-club-39bea06e4f5dcda47c08b89f12df6a24f81233bf.zip | |
add solution week 218 task 1 in perl
Diffstat (limited to 'challenge-218')
| -rw-r--r-- | challenge-218/steven-wilson/perl/ch-01.pl | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-218/steven-wilson/perl/ch-01.pl b/challenge-218/steven-wilson/perl/ch-01.pl new file mode 100644 index 0000000000..29fccf244d --- /dev/null +++ b/challenge-218/steven-wilson/perl/ch-01.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +# You are given a list of 3 or more integers. +# Write a script to find the 3 integers whose product is the maximum and return it. + +use strict; +use warnings; +use Test::More; +use List::Util qw/ product /; + +ok( max_product( 3, 1, 2 ) == 6 ); +ok( max_product( 4, 1, 3, 2 ) == 24 ); +ok( max_product( -1, 0, 1, 3, 1 ) == 3 ); +ok( max_product( -8, 2, -9, 0, -4, 3 ) == 216 ); +done_testing(); + +sub max_product { + my @sorted_list = sort { $a <=> $b } @_; + my $max_last = product( @sorted_list[ -3 .. -1 ] ); + my $max_first = product( @sorted_list[ 0, 1, -1 ] ); + $max_first > $max_last ? return $max_first : return $max_last; +} |
