aboutsummaryrefslogtreecommitdiff
path: root/challenge-202/sgreen/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-202/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-202/sgreen/python/ch-2.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-202/sgreen/python/ch-2.py b/challenge-202/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..a17bd47631
--- /dev/null
+++ b/challenge-202/sgreen/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(n):
+ # This stores the current widest (leftmost) valley
+ solution = []
+
+ for valley in range(len(n)):
+ start = end = valley
+
+ # Get the values to the left that are >= the previous value
+ while start > 0 and n[start] <= n[start-1]:
+ start -= 1
+
+ # Get the values to the right that are >= the previous value
+ while end < len(n)-1 and n[end] <= n[end+1]:
+ end += 1
+
+ # If the len of this solution is larger than the previous, it's a
+ # better solution
+ if len(solution) < end-start:
+ solution = n[start:end+1]
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)