Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hands_on/first/plus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

def plus(x,y):
return x + y
8 changes: 8 additions & 0 deletions hands_on/first/test_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ def test_times_3_string():
result = times_3(value)

assert result == expected

def test_times_3_list():
value = [1]
expected = [1,1,1]

result = times_3(value)

assert result == expected
18 changes: 18 additions & 0 deletions hands_on/first/test_plus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from plus import plus
import numpy as np
from math import isclose

def test_plus_123():
x = 1
y = 2
assert plus(x,y) == 3

def test_plus_1dec1_2dec2_3dec3():
x = 1.1
y = 2.2
assert isclose(plus(x,y), 3.3)

def test_plus_with_numpy():
x = np.array([[1,1],[1,1]])
y = np.array([[2,2],[2,2]])
np.testing.assert_equal(plus(x,y), np.array([[3,3],[3,1]]))
26 changes: 25 additions & 1 deletion hands_on/local_maxima/local_maxima.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,28 @@ def find_maxima(x):
Output:
idx -- list of indices of the local maxima in x
"""
return []
local_maxima = []
for i in range(1, len(x)-1):
first_value = x[i-1]
second_value = x[i]
third_value = x[i+1]
if first_value < second_value and second_value > third_value:
local_maxima.append(i)
# if i==1 & first_value > second_value:
# local_maxima.append(i)
return local_maxima

test_list = [1,3,-2,0,2,1]
print(find_maxima(test_list))

test_list = [4,2,1,3,1,5]
print(find_maxima(test_list))

test_list = []
print(find_maxima(test_list))

test_list = [1,2,2,1]
print(find_maxima(test_list))

test_list = [1,2,2,3,1]
print(find_maxima(test_list))
31 changes: 30 additions & 1 deletion hands_on/local_maxima_part2/local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

def find_maxima(x):
"""Find local maxima of x.

Expand All @@ -7,4 +9,31 @@ def find_maxima(x):
Output:
idx -- list of indices of the local maxima in x
"""
return []
local_maxima = []
print('start')
if len(x)>0:
diff_values = np.diff(x)
if np.any(diff_values==0):
index_of_plateau = np.argwhere(diff_values==0)
if x[int(index_of_plateau)] == np.max(x):
local_maxima.append(int(index_of_plateau))
return local_maxima
for i in range(0, len(x)):
if i==0:
first_value = x[i]
second_value = x[i+1]
if first_value > second_value:
local_maxima.append(i)
elif i==len(x)-1:
first_value = x[i-1]
second_value = x[i]
if first_value < second_value:
local_maxima.append(i)
else:
first_value = x[i-1]
second_value = x[i]
third_value = x[i+1]
if first_value < second_value and second_value > third_value:
local_maxima.append(i)
return local_maxima

11 changes: 9 additions & 2 deletions hands_on/local_maxima_part2/test_local_maxima.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ def test_find_maxima_empty():


def test_find_maxima_plateau():
raise Exception('not yet implemented')
values = [1,2,2,1]
expected_1 = [1]
expected_2 = [2]
maxima = find_maxima(values)
assert maxima == expected_1 or maxima == expected_2


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