diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-09-15 10:03:56 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-09-15 10:03:56 +0100 |
| commit | b7dffbfb668d8ae3c7e9f77ef3c7da40d721da11 (patch) | |
| tree | 319c7626392529ed543608745552f60a1375fc29 /challenge-339 | |
| parent | 1f0dedba8efc34bb49b219ab9f5618eeea5c068b (diff) | |
| download | perlweeklychallenge-club-b7dffbfb668d8ae3c7e9f77ef3c7da40d721da11.tar.gz perlweeklychallenge-club-b7dffbfb668d8ae3c7e9f77ef3c7da40d721da11.tar.bz2 perlweeklychallenge-club-b7dffbfb668d8ae3c7e9f77ef3c7da40d721da11.zip | |
- Added solutions by PokGoPun.
- Added solutions by Feng Chang.
- Added solutions by Niels van Dijke.
- Added solutions by Simon Proctor.
- Added solutions by Mohammad Anwar.
Diffstat (limited to 'challenge-339')
| -rw-r--r-- | challenge-339/mohammad-anwar/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-339/perlboy1967/perl/ch-1.pl (renamed from challenge-339/perlboy1967/perl/ch1.pl) | 0 | ||||
| -rwxr-xr-x | challenge-339/perlboy1967/perl/ch-2.pl (renamed from challenge-339/perlboy1967/perl/ch2.pl) | 0 |
3 files changed, 42 insertions, 0 deletions
diff --git a/challenge-339/mohammad-anwar/perl/ch-1.pl b/challenge-339/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..265579d0a8 --- /dev/null +++ b/challenge-339/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use List::Util qw/max/; + +sub max_diff { + my @nums = @_; + + # For 4 elements, there are only 3 possible pairings + if (@nums == 4) { + my ($a, $b, $c, $d) = @nums; + return max( + $a*$b - $c*$d, + $a*$c - $b*$d, + $a*$d - $b*$c, + ); + } + + # For larger arrays, we need to consider extreme values + my @s = sort {$a <=> $b} @nums; + + # The maximum difference will likely come from: + # Option 1: (two largest) vs (two smallest) + # Option 2: (largest*smallest) vs (second largest*second smallest) + # Option 3: (largest*second smallest) vs (second largest*smallest) + + my $opt1 = $s[-1]*$s[-2] - $s[0]*$s[1]; + my $opt2 = $s[-1]*$s[0] - $s[-2]*$s[1]; + my $opt3 = $s[-1]*$s[1] - $s[-2]*$s[0]; + + return max($opt1, $opt2, $opt3); +} + +is(max_diff(5,9,3,4,6), 42); +is(max_diff(1,-2,3,-4), 10); +is(max_diff(-3,-1,-2,-4), 10); +is(max_diff(10,2,0,5,1), 50); +is(max_diff(7,8,9,10,10), 44); + +done_testing; diff --git a/challenge-339/perlboy1967/perl/ch1.pl b/challenge-339/perlboy1967/perl/ch-1.pl index ed0a6212b0..ed0a6212b0 100755 --- a/challenge-339/perlboy1967/perl/ch1.pl +++ b/challenge-339/perlboy1967/perl/ch-1.pl diff --git a/challenge-339/perlboy1967/perl/ch2.pl b/challenge-339/perlboy1967/perl/ch-2.pl index 63c29a8dc8..63c29a8dc8 100755 --- a/challenge-339/perlboy1967/perl/ch2.pl +++ b/challenge-339/perlboy1967/perl/ch-2.pl |
