aboutsummaryrefslogtreecommitdiff
path: root/challenge-187/izem/python/ch-2.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2022-10-24 17:44:04 +0800
committer冯昶 <fengchang@novel-supertv.com>2022-10-24 17:44:04 +0800
commit7783ecb7dad2ae5ea3d3022a13e3901c196c4154 (patch)
treefa408d0b19c541602d81799c8a13eea61df2cb8f /challenge-187/izem/python/ch-2.py
parenta406c6935f88c7afa04a707a2611ebe4fe4eadae (diff)
parentbb06570f6b1634ca14bfd08927f3bfc6c052c494 (diff)
downloadperlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.tar.gz
perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.tar.bz2
perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-187/izem/python/ch-2.py')
-rw-r--r--challenge-187/izem/python/ch-2.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-187/izem/python/ch-2.py b/challenge-187/izem/python/ch-2.py
new file mode 100644
index 0000000000..2585ae38e0
--- /dev/null
+++ b/challenge-187/izem/python/ch-2.py
@@ -0,0 +1,56 @@
+"""Perl weekly challenge 187 - Task #2: Magical Triplets.
+
+You are given a list of positive numbers, @n, having at least 3 numbers.
+Write a script to find the triplets (a, b, c) from the given list that
+satisfies the following rules
+i.e. 'a + b > c', 'b + c > a', 'a + c > b' and 'a + b + c' is maximum.
+In case, you end up with more than one triplets having the maximum then
+pick the triplet where 'a >= b >= c'.
+
+https://theweeklychallenge.org/blog/perl-weekly-challenge-187/#TASK2
+"""
+import unittest
+from typing import Iterable
+
+
+def magical_triplets(numbers: Iterable[int]) -> Iterable | None:
+ """Return most magical triplet from given numbers.
+
+ :param numbers: numbers to make triplets (at least 3)
+ :return: most magical triplet (or empty)
+ """
+ if len(numbers) < 3:
+ raise ValueError("Input list should be at least 3 numbers.")
+
+ triplet = (0,)
+ for i, a in enumerate(numbers):
+ for j, b in enumerate(numbers):
+ if j == i:
+ continue
+ for k, c in enumerate(numbers):
+ if k == i or k == j:
+ continue
+ if a + b > c and b + c > a and a + c > b \
+ and sum((a, b, c)) >= sum(triplet):
+ if sum((a, b, c)) == sum(triplet) and not a >= b >= c:
+ continue
+ triplet = (a, b, c)
+
+ return triplet if len(triplet) == 3 else None
+
+
+class TestMagicalTriplets(unittest.TestCase):
+
+ def test_magical_triplets(self):
+ cases = (
+ ((1, 2, 3, 2), (3, 2, 2)),
+ ((1, 3, 2), None),
+ ((1, 1, 2, 3), None),
+ ((2, 4, 3), (4, 3, 2)),
+ )
+ for case in cases:
+ self.assertEqual(magical_triplets(case[0]), case[1], f"Input: {case[0]}")
+
+
+if __name__ == '__main__':
+ unittest.main()