diff options
| author | Pok <pok@goyangi> | 2025-09-15 12:31:01 +0700 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-09-15 12:31:01 +0700 |
| commit | e989fb26d543d53bd610250db5125396daeb2477 (patch) | |
| tree | b3ced8b8a2a572865d28ff6a541d94c81b0af2b7 | |
| parent | f78ccc41e5c6cbc1a2c0f5823e6004b703e8ac0c (diff) | |
| download | perlweeklychallenge-club-e989fb26d543d53bd610250db5125396daeb2477.tar.gz perlweeklychallenge-club-e989fb26d543d53bd610250db5125396daeb2477.tar.bz2 perlweeklychallenge-club-e989fb26d543d53bd610250db5125396daeb2477.zip | |
pwc339 solution in python
| -rw-r--r-- | challenge-339/pokgopun/python/ch-1.py | 95 | ||||
| -rw-r--r-- | challenge-339/pokgopun/python/ch-2.py | 111 |
2 files changed, 206 insertions, 0 deletions
diff --git a/challenge-339/pokgopun/python/ch-1.py b/challenge-339/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..b23d18db42 --- /dev/null +++ b/challenge-339/pokgopun/python/ch-1.py @@ -0,0 +1,95 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-339/ +""" + +Task 1: Max Diff + +Submitted by: [42]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). + +Example 1 + +Input: @ints = (5, 9, 3, 4, 6) +Output: 42 + +Pair 1: (9, 6) +Pair 2: (3, 4) +Product Diff: (9 * 6) - (3 * 4) => 54 - 12 => 42 + +Example 2 + +Input: @ints = (1, -2, 3, -4) +Output: 8 + +Pair 1: (3, 1) +Pair 2: (-2, -4) + +Example 3 + +Input: @ints = (-3, -1, -2, -4) +Output: 10 + +Pair 1: (-1, -2) +Pair 2: (-3, -4) + +Example 4 + +Input: @ints = (10, 2, 0, 5, 1) +Output: 50 + +Pair 1: (10, 5) +Pair 2: (0, 1) + +Example 5 + +Input: @ints = (7, 8, 9, 10, 10) +Output: 44 + +Pair 1: (10, 10) +Pair 2: (7, 8) + +Task 2: Peak Point +""" +### solution by pokgopun@gmail.com + +from itertools import combinations + +def md(ints: tuple[int]) -> int: + mx, pair = None, None + pairs = combinations(range(len(ints)),2) + for ps in combinations(pairs,2): + p1, p2 = ps[0], ps[1] + if len(set(p1+p2)) != 4: + continue + v1, v2 = tuple(ints[i] for i in p1), tuple(ints[i] for i in p2) + v = abs(v1[0]*v1[1] - v2[0]*v2[1]) + if mx == None or mx < v: + mx = v + pair = (v1,v2) + #print(pair, mx) + return mx + +import unittest + +class TestMd(unittest.TestCase): + def test(self): + for inpt, otpt in { + (5, 9, 3, 4, 6): 42, + (1, -2, 3, -4): 10, + (-3, -1, -2, -4): 10, + (10, 2, 0, 5, 1): 50, + (7, 8, 9, 10, 10): 44, + }.items(): + self.assertEqual(md(inpt),otpt) + +unittest.main() diff --git a/challenge-339/pokgopun/python/ch-2.py b/challenge-339/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..153058aa04 --- /dev/null +++ b/challenge-339/pokgopun/python/ch-2.py @@ -0,0 +1,111 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-339/ +""" + +Task 2: Peak Point + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of altitude gain. + + Write a script to find the peak point gained. + +Example 1 + +Input: @gain = (-5, 1, 5, -9, 2) +Output: 1 + +start: 0 +1st change: 0 + (-5) = -5 +2nd change: -5 + 1 = -4 +3rd change: -4 + 5 = 1 +4th change: 1 + (-9) = -8 +5th change: -8 + 2 = -6 + +max(0, -5, -4, 1, -8, -6) = 1 + +Example 2 + +Input: @gain = (10, 10, 10, -25) +Output: 30 + +start: 0 +1st change: 0 + 10 = 10 +2nd change: 10 + 10 = 20 +3rd change: 20 + 10 = 30 +4th change: 30 + (-25) = 5 + +max(0, 10, 20, 30, 5) = 30 + +Example 3 + +Input: @gain = (3, -4, 2, 5, -6, 1) +Output: 6 + +start: 0 +1st change: 0 + 3 = 3 +2nd change: 3 + (-4) = -1 +3rd change: -1 + 2 = 1 +4th change: 1 + 5 = 6 +5th change: 6 + (-6) = 0 +6th change: 0 + 1 = 1 + +max(0, 3, -1, 1, 6, 0, 1) = 6 + +Example 4 + +Input: @gain = (-1, -2, -3, -4) +Output: 0 + +start: 0 +1st change: 0 + (-1) = -1 +2nd change: -1 + (-2) = -3 +3rd change: -3 + (-3) = -6 +4th change: -6 + (-4) = -10 + +max(0, -1, -3, -6, -10) = 0 + +Example 5 + +Input: @gain = (-10, 15, 5) +Output: 10 + +start: 0 +1st change: 0 + (-10) = -10 +2nd change: -10 + 15 = 5 +3rd change: 5 + 5 = 10 + +max(0, -10, 5, 10) = 10 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 21st September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def pp(gain: tuple[int]) -> int: + sm = 0 + mx = sm + for n in gain: + sm += n + if mx < sm: + mx = sm + return mx + +import unittest + +class TestPp(unittest.TestCase): + def test(self): + for inpt, otpt in { + (-5, 1, 5, -9, 2): 1, + (10, 10, 10, -25): 30, + (3, -4, 2, 5, -6, 1): 6, + (-1, -2, -3, -4): 0, + (-10, 15, 5): 10, + }.items(): + self.assertEqual(pp(inpt),otpt) + +unittest.main() |
