diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-09-15 23:14:39 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-09-15 23:14:39 +0100 |
| commit | 9b49b3c00a595fbb7441615fea79b7fbc2694c99 (patch) | |
| tree | f75f7e7ff98d0103413a6adf03513ed65dd6777c /challenge-339 | |
| parent | 7f78d729369f524a8bf4d7d9eb693aa689d78af6 (diff) | |
| download | perlweeklychallenge-club-9b49b3c00a595fbb7441615fea79b7fbc2694c99.tar.gz perlweeklychallenge-club-9b49b3c00a595fbb7441615fea79b7fbc2694c99.tar.bz2 perlweeklychallenge-club-9b49b3c00a595fbb7441615fea79b7fbc2694c99.zip | |
- Added solutions by Andreas Mahnke.
- Added solutions by E. Choroba.
- Added solutions by Eric Cheung.
- Added solutions by W. Luis Mochan.
- Added solutions by Peter Campbell Smith.
- Added solutions by Peter Meszaros.
- Added solutions by Thomas Kohler.
Diffstat (limited to 'challenge-339')
| -rwxr-xr-x | challenge-339/eric-cheung/python/ch-1.py | 17 | ||||
| -rwxr-xr-x | challenge-339/eric-cheung/python/ch-2.py | 11 | ||||
| -rw-r--r-- | challenge-339/mohammad-anwar/perl/ch-1.pl | 35 |
3 files changed, 47 insertions, 16 deletions
diff --git a/challenge-339/eric-cheung/python/ch-1.py b/challenge-339/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..9e84200c90 --- /dev/null +++ b/challenge-339/eric-cheung/python/ch-1.py @@ -0,0 +1,17 @@ +
+from math import prod
+
+## Ref.:
+
+## arrInts = [5, 9, 3, 4, 6] ## Example 1
+## arrInts = [1, -2, 3, -4] ## Example 2
+## arrInts = [-3, -1, -2, -4] ## Example 3
+## arrInts = [10, 2, 0, 5, 1] ## Example 4
+arrInts = [7, 8, 9, 10, 10] ## Example 5
+
+arrInts = sorted(arrInts)
+
+if all(nElem >= 0 for nElem in arrInts) or all(nElem <= 0 for nElem in arrInts):
+ print (abs(prod(arrInts[-2:]) - prod(arrInts[:2])))
+else:
+ print (arrInts[-3] * arrInts[-2] - arrInts[0] * arrInts[-1])
diff --git a/challenge-339/eric-cheung/python/ch-2.py b/challenge-339/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..08f9ccac92 --- /dev/null +++ b/challenge-339/eric-cheung/python/ch-2.py @@ -0,0 +1,11 @@ +
+## arrGain = [-5, 1, 5, -9, 2] ## Example 1
+## arrGain = [10, 10, 10, -25] ## Example 2
+## arrGain = [3, -4, 2, 5, -6, 1] ## Example 3
+## arrGain = [-1, -2, -3, -4] ## Example 4
+arrGain = [-10, 15, 5] ## Example 5
+
+arrOutput = [0]
+arrOutput = arrOutput + [sum(arrGain[:nIndx + 1]) for nIndx in range(len(arrGain))]
+
+print (max(arrOutput))
diff --git a/challenge-339/mohammad-anwar/perl/ch-1.pl b/challenge-339/mohammad-anwar/perl/ch-1.pl index 265579d0a8..b6944cacd4 100644 --- a/challenge-339/mohammad-anwar/perl/ch-1.pl +++ b/challenge-339/mohammad-anwar/perl/ch-1.pl @@ -20,23 +20,26 @@ sub max_diff { # 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); + my ($l1, $l2, $l3, $s1, $s2, $s3) = + ($s[-1], $s[-2], $s[-3], $s[0], $s[1], $s[2]); + + my $opt1 = $l1*$l2 - $s1*$s2; + my $opt2 = $l1*$s1 - $l2*$s2; + my $opt3 = $l1*$s2 - $l2*$s1; + my $opt4 = $l1*$l2 - $l1*$s3; + my $opt5 = $s1*$s2 - $l1*$s3; + my $opt6 = $s1*$l3 - $l1*$s1; + my $opt7 = $s2*$s3 - $l1*$s1; + + return max($opt1, $opt2, $opt3, $opt4, $opt5, $opt6, $opt7); } -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); +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); +is(max_diff(-1,0,1,2,3,4,5), 23); +is(max_diff(-10,0,1,2,3,4,5), 62); done_testing; |
