aboutsummaryrefslogtreecommitdiff
path: root/challenge-071/paulo-custodio/python/ch-1.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-20 21:40:27 +0100
committerGitHub <noreply@github.com>2024-09-20 21:40:27 +0100
commitb521af146ef7c2bd338bc5355e2fc8f6c49cbd30 (patch)
tree8d8e24fcdbb56212b91343e1306cce425826dc53 /challenge-071/paulo-custodio/python/ch-1.py
parent962fb60ab30715e2dedc4fda8619042d22fba65e (diff)
parent4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 (diff)
downloadperlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.gz
perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.bz2
perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.zip
Merge pull request #10874 from pauloscustodio/master
Add Python solution to challenge 067
Diffstat (limited to 'challenge-071/paulo-custodio/python/ch-1.py')
-rw-r--r--challenge-071/paulo-custodio/python/ch-1.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-071/paulo-custodio/python/ch-1.py b/challenge-071/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..01cbebfece
--- /dev/null
+++ b/challenge-071/paulo-custodio/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Challenge 071
+#
+# TASK #1 > Peak Element
+# Submitted by: Mohammad S Anwar
+# You are given positive integer $N (>1).
+#
+# Write a script to create an array of size $N with random unique elements
+# between 1 and 50.
+#
+# In the end it should print peak elements in the array, if found.
+#
+# An array element is called peak if it is bigger than it's neighbour.
+#
+# Example 1
+# Array: [ 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ]
+# Peak: [ 48, 45, 21 ]
+# Example 2
+# Array: [ 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ]
+# Peak: [ 47, 32, 39, 36 ]
+
+import sys
+
+def peek_elem(n):
+ out = []
+ if len(n) < 2:
+ return out
+ if n[0] > n[1]:
+ out.append(n[0])
+ for i in range(1, len(n) - 1):
+ if n[i] > n[i-1] and n[i] > n[i+1]:
+ out.append(n[i])
+ if n[-1] > n[-2]:
+ out.append(n[-1])
+ return out
+
+n = list(map(int, sys.argv[1:]))
+print(", ".join(map(str, peek_elem(n))))