diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py new file mode 100644 index 0000000..a6be11d --- /dev/null +++ b/hands_on/local_maxima/local_maxima.py @@ -0,0 +1,16 @@ +import numpy as np + +def local_maxima(numlist): + #numlist = input('enter list') + 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]) +#local_maxima([1,4,-5,0,2,1]) +#local_maxima([-1,-1,0,1]) 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) diff --git a/hands_on/pyanno_voting/test_something.py b/hands_on/pyanno_voting/test_something.py new file mode 100644 index 0000000..38b3829 --- /dev/null +++ b/hands_on/pyanno_voting/test_something.py @@ -0,0 +1,34 @@ +from math import isclose +import numpy +#from numpy import assert_array_equal + +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) + #assert_array_equal(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