From 5834bc8959583d6d7f3c31001916abffef9afecb Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 2 Nov 2025 17:17:41 +0000 Subject: add solution week345 task 1 in python --- challenge-345/steven-wilson/python/ch_1.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-345/steven-wilson/python/ch_1.py diff --git a/challenge-345/steven-wilson/python/ch_1.py b/challenge-345/steven-wilson/python/ch_1.py new file mode 100644 index 0000000000..a07ed939a9 --- /dev/null +++ b/challenge-345/steven-wilson/python/ch_1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + + +def peak_position(ints): + """ Given an array of integers, find all the peaks in the array, a peak is + an element that is strictly greater than its left and right neighbours. + Return the indices of all such peak positions. + >>> peak_position([1, 3, 2]) + [1] + >>> peak_position([2, 4, 6, 5, 3]) + [2] + >>> peak_position([1, 2, 3, 2, 4, 1]) + [2, 4] + >>> peak_position([5, 3, 1]) + [] + >>> peak_position([1, 5, 1, 5, 1, 5, 1]) + [1, 3, 5] + """ + return [n for n, i in enumerate(ints) if n > 0 and n < len(ints) - 1 + and i > ints[n-1] and i > ints[n + 1]] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit