aboutsummaryrefslogtreecommitdiff
path: root/challenge-339/eric-cheung/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-339/eric-cheung/python')
-rwxr-xr-xchallenge-339/eric-cheung/python/ch-1.py17
-rwxr-xr-xchallenge-339/eric-cheung/python/ch-2.py11
2 files changed, 28 insertions, 0 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))