diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-21 19:52:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-21 19:52:50 +0100 |
| commit | 8d57a082d4e93e697a8a2c7478f43eea2e4909f9 (patch) | |
| tree | fef49edfa9ac4f45991141778a2dd5a8e04408da /challenge-071/lubos-kolouch/python/ch-1.py | |
| parent | d6f06ffb2148ff237561667344cd4f8876780a09 (diff) | |
| parent | 4eebc07786114e281c00a6002eb0183daa35e0f4 (diff) | |
| download | perlweeklychallenge-club-8d57a082d4e93e697a8a2c7478f43eea2e4909f9.tar.gz perlweeklychallenge-club-8d57a082d4e93e697a8a2c7478f43eea2e4909f9.tar.bz2 perlweeklychallenge-club-8d57a082d4e93e697a8a2c7478f43eea2e4909f9.zip | |
Merge pull request #8113 from LubosKolouch/master
feat(challenge-071/lubos-kolouch/perl,python/ch-1,-ch-2): Challenge 071 LK Perl Python
Diffstat (limited to 'challenge-071/lubos-kolouch/python/ch-1.py')
| -rw-r--r-- | challenge-071/lubos-kolouch/python/ch-1.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-071/lubos-kolouch/python/ch-1.py b/challenge-071/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7a92d52f8b --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_peaks(arr: List[int]) -> List[int]: + n = len(arr) + # Initialize peaks list with the first element if it's bigger than the second + peaks = [arr[0]] if arr[0] > arr[1] else [] + + # Iterate over the array checking for peaks + for i in range(1, n-1): + if arr[i] > arr[i-1] and arr[i] > arr[i+1]: + peaks.append(arr[i]) + + # Add the last element if it's bigger than the penultimate + if arr[-1] > arr[-2]: + peaks.append(arr[-1]) + + return peaks + + +# Tests +print(find_peaks([18, 45, 38, 25, 10, 7, 21, 6, 28, 48])) # [48, 45, 21] +print(find_peaks([47, 11, 32, 8, 1, 9, 39, 14, 36, 23])) # [47, 32, 39, 36] |
