From 9e8e5ae24bd4d0b379aa3ade71de2303c6d8a94e Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 6 Sep 2022 10:37:57 +0200 Subject: [PATCH 1/2] Implement test_local_maxima_plateau --- hands_on/local_maxima/test_local_maxima.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hands_on/local_maxima/test_local_maxima.py b/hands_on/local_maxima/test_local_maxima.py index 5428945..b8caf68 100644 --- a/hands_on/local_maxima/test_local_maxima.py +++ b/hands_on/local_maxima/test_local_maxima.py @@ -23,5 +23,7 @@ def test_local_maxima_edges(): def test_local_maxima_plateau(): - raise Exception('not yet implemented') - + values = [1,2,2,1] + expected = [1,2] + maxima = local_maxima(values) + assert maxima == expected From e69fce557d73b8e723d4fc0bef53d0ece590c9b6 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 6 Sep 2022 10:38:16 +0200 Subject: [PATCH 2/2] Implement and test local_maxima function with list comprehension --- hands_on/local_maxima/local_maxima.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index 1575179..ded154d 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -19,4 +19,12 @@ def local_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + n = len(x) + + rv = [i for i in range(n) + if i==0 and x[i] > x[i+1] + or i==(n-1) and x[i-1] < x[i] + or 0= x[i+1] + ] + + return rv