fixed find maxima function first 3 #7
4 changed files with 56 additions and 5 deletions
|
@ -1,6 +1,16 @@
|
||||||
|
from first import times_3
|
||||||
|
|
||||||
def test_times_3_integer():
|
def test_times_3_integer():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_times_3_string():
|
def test_times_3_string():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def test_times_3_list():
|
||||||
|
value = [1]
|
||||||
|
expected = [1, 1, 1]
|
||||||
|
|
||||||
|
result = times_3(value)
|
||||||
|
|
||||||
|
assert result == expected
|
|
@ -7,4 +7,22 @@ def find_maxima(x):
|
||||||
Output:
|
Output:
|
||||||
idx -- list of indices of the local maxima in x
|
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))
|
||||||
|
|
|
@ -1 +1,12 @@
|
||||||
# Your code goes here
|
|
||||||
|
def f(x,r):
|
||||||
|
return r * x * (1-x)
|
||||||
|
|
||||||
|
def iterate_f(it, x, r):
|
||||||
|
result = [x]
|
||||||
|
|
||||||
|
for i in range(it):
|
||||||
|
x = f(x, r)
|
||||||
|
result.append(x)
|
||||||
|
|
||||||
|
return result
|
|
@ -1,6 +1,6 @@
|
||||||
from numpy.testing import assert_allclose
|
from numpy.testing import assert_allclose
|
||||||
|
from logistic import f, iterate_f
|
||||||
from logistic import f
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_f_corner_cases():
|
def test_f_corner_cases():
|
||||||
|
@ -19,6 +19,11 @@ def test_f_corner_cases():
|
||||||
# x=0.2, r=3.4 => f(x, r)=0.544
|
# x=0.2, r=3.4 => f(x, r)=0.544
|
||||||
# x=0.5, r=2 => f(x, r)=0.5
|
# 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(x, r, expected):
|
||||||
|
result = f(x, r)
|
||||||
|
assert_allclose(result, expected)
|
||||||
|
|
||||||
# Hands on 2:
|
# Hands on 2:
|
||||||
# parametrize the above test using @pytest.mark.parametrize
|
# parametrize the above test using @pytest.mark.parametrize
|
||||||
|
@ -28,4 +33,11 @@ def test_f_corner_cases():
|
||||||
# Implement a function iterate_f that runs f for it iterations. Write tests for the following 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.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.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)
|
Loading…
Add table
Add a link
Reference in a new issue