From c62e1e879724e6394a15ac0726275b7ceb55283a Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:30:59 +0300 Subject: [PATCH 1/2] added local maxima function --- hands_on/first/test_first.py | 9 +++-- hands_on/local_maxima/local_maxima.py | 10 ++++-- hands_on/local_maxima/test_local_maxima.py | 41 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 hands_on/local_maxima/test_local_maxima.py diff --git a/hands_on/first/test_first.py b/hands_on/first/test_first.py index 5ac7354..2dd44cc 100644 --- a/hands_on/first/test_first.py +++ b/hands_on/first/test_first.py @@ -1,6 +1,11 @@ +from first import times_3 + def test_times_3_integer(): - pass + result = times_3([1]) + assert result == [1, 1, 1] def test_times_3_string(): - pass + result = times_3([1]) + assert result == [2] + diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index cfd0a9e..1de0b20 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -4,12 +4,18 @@ def find_maxima(x): Input arguments: x -- 1D list of real numbers + Output: idx -- list of indices of the local maxima in x """ - return [] + maxima = [] + for index in range(len(x)-2): + if x[index+1] > x[index] and x[index +1] > x[index+2]: + maxima.append(index+1) + + return maxima if __name__ == "__main__": - x = [1, 2, 3] + x = [1, 2, 2, 1] print(find_maxima(x)) diff --git a/hands_on/local_maxima/test_local_maxima.py b/hands_on/local_maxima/test_local_maxima.py new file mode 100644 index 0000000..3a16af1 --- /dev/null +++ b/hands_on/local_maxima/test_local_maxima.py @@ -0,0 +1,41 @@ +from local_maxima import find_maxima + + + +def test_local_maxima_empty(): + value = [] + expected = [] + + result = find_maxima(value) + + assert result == expected + + +def test_local_maxima_plateau(): + value = [1, 2, 2, 1] + expected = [] + + result = find_maxima(value) + + assert result == expected + +def test_local_maxima_array1(): + value = [1, 3, 5, 2, 1] + expected = [2] + + result = find_maxima(value) + + assert result == expected + + +def test_local_maxima_array2(): + value = [5, 4, 4, 5] + expected = [] + + result = find_maxima(value) + + assert result == expected + + + + -- 2.39.5 From 0900ac61d4a24c237540a9f54e81d9ac24b3555d Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:58:32 +0300 Subject: [PATCH 2/2] added and tested rabbit logistics function --- testing_project/logistic.py | 2 ++ testing_project/test_logistic.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/testing_project/logistic.py b/testing_project/logistic.py index e49d1c2..33561cb 100644 --- a/testing_project/logistic.py +++ b/testing_project/logistic.py @@ -1 +1,3 @@ # Your code goes here +def f(x, r): + return x*r*(1 -x) \ No newline at end of file diff --git a/testing_project/test_logistic.py b/testing_project/test_logistic.py index 100d824..d18f74f 100644 --- a/testing_project/test_logistic.py +++ b/testing_project/test_logistic.py @@ -18,6 +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: -- 2.39.5