diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index db89ba3..7857b7c 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -7,4 +7,27 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + maxima = [] + for idx in range(len(x)): + if idx == 0: + if x[idx] >= x[idx+1]: + maxima.append(idx) + elif idx == len(x)-1: + if x[idx] >= x[idx-1]: + maxima.append(idx) + else: + if x[idx] >= x[idx+1] and x[idx] >= x[idx-1]: + maxima.append(idx) + return maxima + + +if __name__ == "__main__": + values = [ + [1, 3, -2, 0, 2, 1], + [4,2,1,3,1,5], + [], + [1,2,2,1], + [1,2,2,3,1], + ] + for value in values: + print(find_maxima(value)) diff --git a/hands_on/local_maxima_part2/test_local_maxima.py b/hands_on/local_maxima_part2/test_local_maxima.py index 316442d..aa2872c 100644 --- a/hands_on/local_maxima_part2/test_local_maxima.py +++ b/hands_on/local_maxima_part2/test_local_maxima.py @@ -23,8 +23,14 @@ def test_find_maxima_empty(): def test_find_maxima_plateau(): - raise Exception('not yet implemented') - + values = [1,2,2,1] + expected = [1,2] + maxima = find_maxima(values) + assert maxima == expected + def test_find_maxima_not_a_plateau(): - raise Exception('not yet implemented') + values = [1,2,2,3,1] + expected = [3] + maxima = find_maxima(values) + assert maxima == expected diff --git a/testing_project/logistic.py b/testing_project/logistic.py index e49d1c2..a7926e7 100644 --- a/testing_project/logistic.py +++ b/testing_project/logistic.py @@ -1 +1,2 @@ -# Your code goes here +def f(x, r): + return r*x*(1-x) diff --git a/testing_project/test_logistic.py b/testing_project/test_logistic.py index 100d824..ad36c60 100644 --- a/testing_project/test_logistic.py +++ b/testing_project/test_logistic.py @@ -18,7 +18,16 @@ def test_f_corner_cases(): # x=0.1, r=2.2 => f(x, r)=0.198 # x=0.2, r=3.4 => f(x, r)=0.544 # x=0.5, r=2 => f(x, r)=0.5 - +def test_f_generic_cases(): + # Test cases are (x, r, expected) + cases = [ + (0.1, 2.2, 0.198), + (0.2, 3.4, 0.544), + (0.5, 2, 0.5), + ] + for x, r, expected in cases: + result = f(x, r) + assert_allclose(result, expected) # Hands on 2: # parametrize the above test using @pytest.mark.parametrize