From d03243efe463ac9c250f83ac41cf2066d653ee00 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:29:50 +0300 Subject: [PATCH 1/4] fixed find maxima function first 3 --- hands_on/first/test_first.py | 10 ++++++++++ hands_on/local_maxima_part2/local_maxima.py | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/hands_on/first/test_first.py b/hands_on/first/test_first.py index 5ac7354..a57c562 100644 --- a/hands_on/first/test_first.py +++ b/hands_on/first/test_first.py @@ -1,6 +1,16 @@ +from first import times_3 + def test_times_3_integer(): pass def test_times_3_string(): pass + +def test_times_3_list(): + value = [1] + expected = [1, 1, 1] + + result = times_3(value) + + assert result == expected \ No newline at end of file diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index db89ba3..e36cf3c 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -7,4 +7,22 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + localmax = [] + + for i, num in enumerate(x): + if i == 0: + if x[i+1] < x[i]: + localmax.append(i) + elif i == len(x)-1: + if x[i-1] < x[i]: + localmax.append(i) + else: + if x[i+1] < x[i] and x[i-1] < x[i]: + localmax.append(i) + + return localmax + + +if __name__ == "__main__": + x = [1, 2, 3] + print(find_maxima(x)) -- 2.39.5 From ea7ab8af75b3916d2ae3d632343a27a87379ee01 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:57:00 +0300 Subject: [PATCH 2/4] add new test cases --- testing_project/test_logistic.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/testing_project/test_logistic.py b/testing_project/test_logistic.py index 100d824..b66d476 100644 --- a/testing_project/test_logistic.py +++ b/testing_project/test_logistic.py @@ -20,6 +20,17 @@ def test_f_corner_cases(): # x=0.5, r=2 => f(x, r)=0.5 +def test_f_other_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 -- 2.39.5 From 671fcd2145a326ebd7fa66f28aa017e4a80462c7 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:59:10 +0300 Subject: [PATCH 3/4] add new test case function --- testing_project/logistic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/testing_project/logistic.py b/testing_project/logistic.py index e49d1c2..5e7a44d 100644 --- a/testing_project/logistic.py +++ b/testing_project/logistic.py @@ -1 +1,4 @@ -# Your code goes here +def f(x,r): + return r * x * (1-x) + + \ No newline at end of file -- 2.39.5 From 5205ba24f241a25df91cab837a0a22ec82263045 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 19:41:37 +0300 Subject: [PATCH 4/4] updated functions --- testing_project/logistic.py | 10 +++++++++- testing_project/test_logistic.py | 27 ++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/testing_project/logistic.py b/testing_project/logistic.py index 5e7a44d..a58ad5b 100644 --- a/testing_project/logistic.py +++ b/testing_project/logistic.py @@ -1,4 +1,12 @@ + def f(x,r): return r * x * (1-x) - \ No newline at end of file +def iterate_f(it, x, r): + result = [x] + + for i in range(it): + x = f(x, r) + result.append(x) + + return result \ No newline at end of file diff --git a/testing_project/test_logistic.py b/testing_project/test_logistic.py index b66d476..e55e3ee 100644 --- a/testing_project/test_logistic.py +++ b/testing_project/test_logistic.py @@ -1,6 +1,6 @@ from numpy.testing import assert_allclose - -from logistic import f +from logistic import f, iterate_f +import pytest def test_f_corner_cases(): @@ -19,17 +19,11 @@ def test_f_corner_cases(): # x=0.2, r=3.4 => f(x, r)=0.544 # x=0.5, r=2 => f(x, r)=0.5 +@pytest.mark.parametrize('x, r, expected', [(0.1, 2.2, 0.198), (0.2, 3.4, 0.544), (0.5, 2, 0.5)]) -def test_f_other_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) +def test_f_other_cases(x, r, expected): + result = f(x, r) + assert_allclose(result, expected) # Hands on 2: # parametrize the above test using @pytest.mark.parametrize @@ -39,4 +33,11 @@ def test_f_other_cases(): # Implement a function iterate_f that runs f for it iterations. Write tests for the following cases: # x=0.1, r=2.2, it=1 => iterate_f(it, x, r)=[0.1, 0.198] # x=0.2, r=3.4, it=4 => iterate_f(it, x, r)=[0.2, 0.544, 0.843418, 0.449019, 0.841163] -# x=0.5, r=2, it=3 => iterate_f(it, x, r)=[0.5, 0.5, 0.5] +# x=0.5, r=2, it=3 => iterate_f(it, x, r)=[0.5, 0.5, 0.5, 0.5] + +@pytest.mark.parametrize('x, r, it, expected', [(0.1, 2.2, 1, [0.1, 0.198]), (0.2, 3.4, 4, [0.2, 0.544, 0.843418, 0.449019, 0.841163]), ( + 0.5, 2, 3, [0.5, 0.5, 0.5, 0.5])]) + +def test_iterate_f(x, r, it, expected): + result = iterate_f(it, x, r) + assert_allclose(result, expected, rtol=5e-07, atol=5e-07) \ No newline at end of file -- 2.39.5