aboutsummaryrefslogtreecommitdiff
path: root/challenge-061/paulo-custodio/python/ch-1.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-18 19:59:32 +0100
committerGitHub <noreply@github.com>2024-09-18 19:59:32 +0100
commit85073f31dae3decce780d1fd66cdcb79c82c7f65 (patch)
tree318d3bc02836f4cde215f7f15e9ec875c74a1490 /challenge-061/paulo-custodio/python/ch-1.py
parentbfe139d559d498bcf1ae48cc08520bfc99ee9b85 (diff)
parent51c035e04c5ae5f9f02555206f57eb675a737617 (diff)
downloadperlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.gz
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.bz2
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.zip
Merge pull request #10860 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-061/paulo-custodio/python/ch-1.py')
-rw-r--r--challenge-061/paulo-custodio/python/ch-1.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-061/paulo-custodio/python/ch-1.py b/challenge-061/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..47e232e6aa
--- /dev/null
+++ b/challenge-061/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 061
+#
+# TASK #1 > Product SubArray
+# Reviewed by: Ryan Thompson
+# Given a list of 4 or more numbers, write a script to find the contiguous
+# sublist that has the maximum product. The length of the sublist is irrelevant;
+# your job is to maximize the product.
+#
+# Example
+# Input: [ 2, 5, -1, 3 ]
+#
+# Output: [ 2, 5 ] which gives maximum product 10.
+
+import sys
+
+def product(nums):
+ prod = 1
+ for x in nums:
+ prod *= x
+ return x
+
+
+n = list(map(int, sys.argv[1:]))
+max_sublist = []
+max_prod = int(-1e6)
+
+for i in range(0, len(n)):
+ for j in range(i, len(n)):
+ sublist = n[i:j+1]
+ prod = product(sublist)
+ if prod > max_prod:
+ max_sublist, max_prod = sublist, prod
+
+print(", ".join(list(map(str, max_sublist))))