aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-09-21 20:24:47 +1000
committerSimon Green <mail@simon.green>2025-09-21 20:24:47 +1000
commit4f1da4a5b76906e1656f32648baf794fe9adfb80 (patch)
tree61b5a8340af293d4468b8dd8df17f5d3d3a01928
parentf78ccc41e5c6cbc1a2c0f5823e6004b703e8ac0c (diff)
downloadperlweeklychallenge-club-4f1da4a5b76906e1656f32648baf794fe9adfb80.tar.gz
perlweeklychallenge-club-4f1da4a5b76906e1656f32648baf794fe9adfb80.tar.bz2
perlweeklychallenge-club-4f1da4a5b76906e1656f32648baf794fe9adfb80.zip
sgreen solutions to challenge 339
-rw-r--r--challenge-339/sgreen/README.md4
-rw-r--r--challenge-339/sgreen/blog.txt1
-rwxr-xr-xchallenge-339/sgreen/perl/ch-1.pl30
-rwxr-xr-xchallenge-339/sgreen/perl/ch-2.pl21
-rwxr-xr-xchallenge-339/sgreen/python/ch-1.py42
-rwxr-xr-xchallenge-339/sgreen/python/ch-2.py35
-rwxr-xr-xchallenge-339/sgreen/python/test.py25
7 files changed, 156 insertions, 2 deletions
diff --git a/challenge-339/sgreen/README.md b/challenge-339/sgreen/README.md
index 65d76b613e..a543fc1e96 100644
--- a/challenge-339/sgreen/README.md
+++ b/challenge-339/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 338
+# The Weekly Challenge 339
-Blog: [The Highest Distance](https://dev.to/simongreennet/weekly-challenge-the-highest-distance-4ec)
+Blog: [Maximum climb](https://dev.to/simongreennet/weekly-challenge-maximum-climb-28i3)
diff --git a/challenge-339/sgreen/blog.txt b/challenge-339/sgreen/blog.txt
new file mode 100644
index 0000000000..ed45394204
--- /dev/null
+++ b/challenge-339/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-maximum-climb-28i3 \ No newline at end of file
diff --git a/challenge-339/sgreen/perl/ch-1.pl b/challenge-339/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..24defcb51a
--- /dev/null
+++ b/challenge-339/sgreen/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw(max);
+
+sub main (@ints) {
+ my $solution = 0;
+
+ # Compute all combinations of 4 integers
+ my $iter = Algorithm::Combinatorics::combinations( \@ints, 4 );
+ while ( my $c = $iter->next ) {
+ # Calculate the maximum difference for this combination
+ my $diff = max(
+ abs( ( $c->[0] * $c->[1] ) - ( $c->[2] * $c->[3] ) ),
+ abs( ( $c->[0] * $c->[2] ) - ( $c->[1] * $c->[3] ) ),
+ abs( ( $c->[0] * $c->[3] ) - ( $c->[1] * $c->[2] ) ),
+ );
+ if ( $diff > $solution ) {
+ $solution = $diff;
+ }
+ }
+ say $solution;
+}
+
+main(@ARGV);
diff --git a/challenge-339/sgreen/perl/ch-2.pl b/challenge-339/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..abd4650a47
--- /dev/null
+++ b/challenge-339/sgreen/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ my $current_altitude = 0;
+ my $peak = 0;
+
+ for my $gain (@ints) {
+ $current_altitude += $gain;
+ if ( $current_altitude > $peak ) {
+ $peak = $current_altitude;
+ }
+ }
+ say $peak;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-339/sgreen/python/ch-1.py b/challenge-339/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..71e8009a2e
--- /dev/null
+++ b/challenge-339/sgreen/python/ch-1.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+import sys
+from itertools import combinations
+
+
+def max_diff(ints: list) -> int:
+ """
+ Given a list of integers, find the maximum difference between the products
+ of any two pairs of integers from the list.
+
+ Args:
+ ints (list): List of integers.
+
+ Returns:
+ int: Maximum difference between products of two pairs.
+ """
+ solution = 0
+
+ # Compute all combinations of 4 integers
+ for c in combinations(ints, 4):
+ # Calculate the maximum difference for this combination
+ diff = max(
+ abs((c[0] * c[1]) - (c[2] * c[3])),
+ abs((c[0] * c[2]) - (c[1] * c[3])),
+ abs((c[0] * c[3]) - (c[1] * c[2])),
+ )
+ if diff > solution:
+ solution = diff
+
+ return solution
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = max_diff(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-339/sgreen/python/ch-2.py b/challenge-339/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..2c0e42471a
--- /dev/null
+++ b/challenge-339/sgreen/python/ch-2.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def peak_point(gains: list) -> int:
+ """
+ Calculate the highest altitude reached given a list of altitude gains.
+
+ Args:
+ gains (list): A list of integers representing altitude gains.
+
+ Returns:
+ int: The highest altitude reached.
+ """
+ current_altitude = 0
+ peak = 0
+
+ for gain in gains:
+ current_altitude += gain
+ if current_altitude > peak:
+ peak = current_altitude
+
+ return peak
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = peak_point(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-339/sgreen/python/test.py b/challenge-339/sgreen/python/test.py
new file mode 100755
index 0000000000..3b3cc47ddc
--- /dev/null
+++ b/challenge-339/sgreen/python/test.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.max_diff([5, 9, 3, 4, 6]), 42)
+ self.assertEqual(ch_1.max_diff([1, -2, 3, -4]), 10)
+ self.assertEqual(ch_1.max_diff([-3, -1, -2, -4]), 10)
+ self.assertEqual(ch_1.max_diff([10, 2, 0, 5, 1]), 50)
+ self.assertEqual(ch_1.max_diff([7, 8, 9, 10, 10]), 44)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.peak_point([-5, 1, 5, -9, 2]), 1)
+ self.assertEqual(ch_2.peak_point([10, 10, 10, -25]), 30)
+ self.assertEqual(ch_2.peak_point([3, -4, 2, 5, -6, 1]), 6)
+ self.assertEqual(ch_2.peak_point([-1, -2, -3, -4]), 0)
+ self.assertEqual(ch_2.peak_point([-10, 15, 5]), 10)
+
+
+if __name__ == '__main__':
+ unittest.main()