diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2025-09-15 08:04:55 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2025-09-15 08:04:55 +0000 |
| commit | 084cee0f32d8edca85f24fecaff7bbdaa0853896 (patch) | |
| tree | f3cced3b3e7ad7aaff26ae7e121c61ee57e2af49 | |
| parent | f78ccc41e5c6cbc1a2c0f5823e6004b703e8ac0c (diff) | |
| download | perlweeklychallenge-club-084cee0f32d8edca85f24fecaff7bbdaa0853896.tar.gz perlweeklychallenge-club-084cee0f32d8edca85f24fecaff7bbdaa0853896.tar.bz2 perlweeklychallenge-club-084cee0f32d8edca85f24fecaff7bbdaa0853896.zip | |
w339 - Task 1 & 2
| -rwxr-xr-x | challenge-339/perlboy1967/perl/ch1.pl | 71 | ||||
| -rwxr-xr-x | challenge-339/perlboy1967/perl/ch2.pl | 35 |
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-339/perlboy1967/perl/ch1.pl b/challenge-339/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..ed0a6212b0 --- /dev/null +++ b/challenge-339/perlboy1967/perl/ch1.pl @@ -0,0 +1,71 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-339#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Max Diff +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers having four or more elements. + +Write a script to find two pairs of numbers from this list (four numbers total) +so that the difference between their products is as large as possible. + +In the end return the max difference. + +|| With Two pairs (a, b) and (c, d), the product difference is (a * b) - (c * d). + + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::Util qw(min); + +sub maxDiff (@ints) { + my @i = sort { $a <=> $b } @ints; + my (@n,@p,$max); + + # Collect positive and not-positive numbers + for (@i) { + push(@n,$_) if $_ <= 0; + unshift(@p,$_) if $_ > 0; + } + + # First find our max and reduce its source list + if (@p > 1 and @n > 1) { + if ($p[0]*$p[1] >= $n[0]*$n[1]) { + $max = (shift @p) * (shift @p); + } else { + $max = (shift @n) * (shift @n); + } + } elsif (@p > 1) { + $max = (shift @p) * (shift @p); + } elsif (@n > 1) { + $max = (shift @n) * (shift @n); + } else { + $max = (shift @n) * pop(@p); + } + + # Find the minimum with left over data + my @min; + push(@min,$n[-1]*$p[-1]) if (@n and @p); + push(@min,$p[-1]*$p[-2]) if @p >= 2; + push(@min,$n[-1]*$n[-2]) if @n >= 2; + + return $max - min(@min) +} + +is(maxDiff(5,9,3,4,6),42,'Example 1'); +is(maxDiff(1,-2,3,-4),5,'Example 2'); +is(maxDiff(-3,-1,-2,-4),10,'Example 3'); +is(maxDiff(10,2,0,5,1),50,'Example 4'); +is(maxDiff(7,8,9,10,10),44,'Example 5'); +is(maxDiff(0,1,2,3),6,'Own example 1'); +is(maxDiff(-2,-1,0,1,2),2,'Own example 2'); + +done_testing; diff --git a/challenge-339/perlboy1967/perl/ch2.pl b/challenge-339/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..63c29a8dc8 --- /dev/null +++ b/challenge-339/perlboy1967/perl/ch2.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-339#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Peak Point +Submitted by: Mohammad Sajid Anwar + +You are given an array of altitude gain. + +Write a script to find the peak point gained. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::Util qw(max); +use List::MoreUtils qw(slide); + +sub peakPoint (@numbers) { + my $i = 0; + max map { $i += $_ } @numbers; +} + +is(peakPoint(-5,1,5,-9,2),1,'Example 1'); +is(peakPoint(10,10,10,-25),30,'Example 2'); +is(peakPoint(3,-4,2,5,-6,1),6,'Example 3'); +is(peakPoint(-1,-2,-3,-4),-1,'Example 4'); +is(peakPoint(-10,15,5),10,'Example 5'); + +done_testing; |
