aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-345/vinod-k/python/ch-1.py20
-rw-r--r--challenge-345/vinod-k/python/ch-2.py31
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-345/vinod-k/python/ch-1.py b/challenge-345/vinod-k/python/ch-1.py
new file mode 100644
index 0000000000..509112d6e3
--- /dev/null
+++ b/challenge-345/vinod-k/python/ch-1.py
@@ -0,0 +1,20 @@
+def find_peaks(ints):
+ peaks = []
+ for i in range(len(ints)):
+ left = float('-inf') if i == 0 else ints[i-1]
+ right = float('-inf') if i == len(ints)-1 else ints[i+1]
+ if ints[i] > left and ints[i] > right:
+ peaks.append(i)
+ return peaks
+
+examples = [
+ [1, 3, 2],
+ [2, 4, 6, 5, 3],
+ [1, 2, 3, 2, 4, 1],
+ [5, 3, 1],
+ [1, 5, 1, 5, 1, 5, 1],
+]
+
+for ex in examples:
+ print(f"Input: {ex}")
+ print(f"Output: {find_peaks(ex)}\n")
diff --git a/challenge-345/vinod-k/python/ch-2.py b/challenge-345/vinod-k/python/ch-2.py
new file mode 100644
index 0000000000..06cddc1a4d
--- /dev/null
+++ b/challenge-345/vinod-k/python/ch-2.py
@@ -0,0 +1,31 @@
+def process_ints(ints):
+ seen = []
+ ans = []
+ i = 0
+ while i < len(ints):
+ if ints[i] == -1:
+ x = 0
+ j = i - 1
+ while j >= 0 and ints[j] == -1:
+ x += 1
+ j -= 1
+ if x < len(seen):
+ ans.append(seen[x])
+ else:
+ ans.append(-1)
+ else:
+ seen.insert(0, ints[i])
+ i += 1
+ return ans
+
+examples = [
+ [5, -1, -1], # Output: [5, -1]
+ [3, 7, -1, -1, -1], # Output: [7, 3, -1]
+ [2, -1, 4, -1, -1], # Output: [2, 4, 2]
+ [10, 20, -1, 30, -1, -1], # Output: [20, 30, 20]
+ [-1, -1, 5, -1], # Output: [-1, -1, 5]
+]
+
+for ex in examples:
+ print(f"Input: {ex}")
+ print(f"Output: {process_ints(ex)}\n")