diff options
| author | vinodk89 <vinodkk89@gmail.com> | 2025-11-01 18:36:26 +0530 |
|---|---|---|
| committer | vinodk89 <vinodkk89@gmail.com> | 2025-11-01 18:36:26 +0530 |
| commit | 4d7080b136537c7cb2421154cf8b0cb0198d6dbf (patch) | |
| tree | 1b4e55885e391161d7d0a7ca272c47b34083c365 | |
| parent | 381782de74b3cafbcfc6faad89db7712bbba7f18 (diff) | |
| download | perlweeklychallenge-club-4d7080b136537c7cb2421154cf8b0cb0198d6dbf.tar.gz perlweeklychallenge-club-4d7080b136537c7cb2421154cf8b0cb0198d6dbf.tar.bz2 perlweeklychallenge-club-4d7080b136537c7cb2421154cf8b0cb0198d6dbf.zip | |
Solutions for Challenge-345 (Python)
| -rw-r--r-- | challenge-345/vinod-k/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-345/vinod-k/python/ch-2.py | 31 |
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") |
