From e2910130e8ae2f268b555a6f5cb752eebae3ffc1 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 09:29:13 +0200 Subject: [PATCH 1/5] local maxima of list --- hands_on/local_maxima/local_maxima.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 hands_on/local_maxima/local_maxima.py diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py new file mode 100644 index 0000000..2a834f9 --- /dev/null +++ b/hands_on/local_maxima/local_maxima.py @@ -0,0 +1,13 @@ +import numpy as np + +def local_maxima(numlist): + loc = 0 + curr = numlist[0] + for idx, elem in enumerate(numlist): + if elem>curr: + loc = idx + curr = elem + print(loc) + return loc + +#local_maxima([1, 3, -1, 2, 1]) From aa113f51bb4505cf31a5197a2750056581682142 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 09:51:26 +0200 Subject: [PATCH 2/5] add some test cases --- hands_on/local_maxima/local_maxima.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index 2a834f9..a6be11d 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -1,6 +1,7 @@ import numpy as np def local_maxima(numlist): + #numlist = input('enter list') loc = 0 curr = numlist[0] for idx, elem in enumerate(numlist): @@ -11,3 +12,5 @@ def local_maxima(numlist): return loc #local_maxima([1, 3, -1, 2, 1]) +#local_maxima([1,4,-5,0,2,1]) +#local_maxima([-1,-1,0,1]) From e3812e85f8b39fcf20b6e49618ec63b4db1807b3 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 10:33:42 +0200 Subject: [PATCH 3/5] testing tests --- hands_on/pyanno_voting/test_something.py | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 hands_on/pyanno_voting/test_something.py diff --git a/hands_on/pyanno_voting/test_something.py b/hands_on/pyanno_voting/test_something.py new file mode 100644 index 0000000..b676ed9 --- /dev/null +++ b/hands_on/pyanno_voting/test_something.py @@ -0,0 +1,32 @@ +from math import isclose +import numpy + +def test_floating(): + assert isclose(1.1 + 2.2, 3.3) + +#def test_tol(): +# assert isclose(1.1 + 2.2, abs_tol=0.1) + +def test_arraysum(): + x = numpy.array([1,1]) + y = numpy.array([2,2]) + z = numpy.array([3,3]) + assert all(x + y == z) + + +def test_arithmetic(): + assert 1 == 1 + assert 2 * 3 == 6 + +def test_len_list(): + lst = ['a', 'b', 'c'] + assert len(lst) == 3 + +def test_equalthree(): + assert 1 + 2 == 3 + +def test_truthy(): + assert 1 + +#def test_floatsum(): +# assert 1.1 + 2.2 == 3.3 From 4eef200a17646dea6987fa2dac9a3880c4f5c26a Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 11:08:18 +0200 Subject: [PATCH 4/5] testing tests floats and arrays --- hands_on/pyanno_voting/test_something.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hands_on/pyanno_voting/test_something.py b/hands_on/pyanno_voting/test_something.py index b676ed9..38b3829 100644 --- a/hands_on/pyanno_voting/test_something.py +++ b/hands_on/pyanno_voting/test_something.py @@ -1,5 +1,6 @@ from math import isclose import numpy +#from numpy import assert_array_equal def test_floating(): assert isclose(1.1 + 2.2, 3.3) @@ -12,6 +13,7 @@ def test_arraysum(): y = numpy.array([2,2]) z = numpy.array([3,3]) assert all(x + y == z) + #assert_array_equal(x + y, z) def test_arithmetic(): From 2c70650acee7066d253b7810fb225475e146bd58 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 15:15:28 +0200 Subject: [PATCH 5/5] Fixes:6 --- hands_on/numerical_fuzzing/test_var.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 hands_on/numerical_fuzzing/test_var.py diff --git a/hands_on/numerical_fuzzing/test_var.py b/hands_on/numerical_fuzzing/test_var.py new file mode 100644 index 0000000..d034321 --- /dev/null +++ b/hands_on/numerical_fuzzing/test_var.py @@ -0,0 +1,22 @@ +#Create a directory called numerical_fuzzing in hands_on. +#In a module called test_var.py, write two tests for the variance function, numpy.var: +# First, a deterministic test +# Then, a numerical fuzzing test +from math import isclose +import numpy +from numpy import testing + +def test_var_deterministic(): + x = numpy.array([-2.0, 2.0, 6.0]) + expected = 10.6 + assert isclose(numpy.var(x), expected, abs_tol = 0.1) + +def test_var_fuzzing(): + rand_state = numpy.random.RandomState(1333) + N, D = 100000, 5 + #Goal var: [0.1 , 0.45, 0.8 , 1.15, 1.5] + expected = numpy.linspace(0.1, 1.5, D) + # Generate random, D-dimensional data with the desired mean + x = rand_state.randn(N, D) * expected + vars = numpy.var(x, axis=0) + numpy.testing.assert_allclose(vars, expected*expected, rtol=1e-2)