Testing Class Material
This commit is contained in:
		
						commit
						05b1f6cdd5
					
				
					 85 changed files with 102796 additions and 0 deletions
				
			
		|  | @ -0,0 +1,67 @@ | |||
| import math | ||||
| 
 | ||||
| import numpy as np | ||||
| 
 | ||||
| 
 | ||||
| def find_maxima(x): | ||||
|     """Find local maxima of x. | ||||
| 
 | ||||
|     Input arguments: | ||||
|     x -- 1D list of real numbers | ||||
| 
 | ||||
|     Output: | ||||
|     idx -- list of indices of the local maxima in x | ||||
|     """ | ||||
|     maxima = [] | ||||
|     len_x = len(x) | ||||
|     if len_x == 0: | ||||
|         return maxima | ||||
|     elif len_x == 1: | ||||
|         return [0] | ||||
| 
 | ||||
|     # additional checks | ||||
|     if np.all([isinstance(item, str) for item in x]): | ||||
|         x = [item.lower() for item in x] | ||||
|     if np.all([item == x[0] for item in x]): | ||||
|         return [0] | ||||
| 
 | ||||
|     maxima = check_first_element(x, maxima) | ||||
|     # Check numbers in between | ||||
|     i = 1 | ||||
|     while i < len_x - 1: | ||||
|         if x[i] >= x[i - 1]: | ||||
|             # We have found a potential maximum or start of a plateau | ||||
|             # breakpoint() | ||||
|             if i == 1 and x[i] == x[i - 1]: | ||||
|                 plateau_start = i - 1 | ||||
|             else: | ||||
|                 plateau_start = i | ||||
|             while i < len_x - 1 and x[i] == x[i + 1]: | ||||
|                 i += 1 | ||||
|             plateau_end = i | ||||
|             if plateau_end == len_x - 1: | ||||
|                 maxima.append((plateau_end + plateau_start) // 2) | ||||
|             elif x[plateau_end] > x[plateau_end + 1]: | ||||
|                 maxima.append((plateau_end + plateau_start) // 2) | ||||
|         i += 1 | ||||
|     maxima = check_last_element(x, maxima) | ||||
| 
 | ||||
|     return maxima | ||||
| 
 | ||||
| 
 | ||||
| def check_first_element(x, maxima): | ||||
|     if x[0] > x[1]: | ||||
|         maxima.append(0) | ||||
|     return maxima | ||||
| 
 | ||||
| 
 | ||||
| def check_last_element(x, maxima): | ||||
|     if x[-1] > x[-2]: | ||||
|         maxima.append(len(x) - 1) | ||||
|     return maxima | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     # result = find_maxima([1, 3, -2, 0, 2, 1]) | ||||
|     result = find_maxima([1, 2, 2, 1]) | ||||
|     print(result) | ||||
|  | @ -0,0 +1,97 @@ | |||
| import numpy as np | ||||
| import pytest | ||||
| 
 | ||||
| from local_maxima_solution import find_maxima | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima(): | ||||
|     values = [1, 3, -2, 0, 2, 1] | ||||
|     expected = [1, 4] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_edges(): | ||||
|     values = [4, 2, 1, 0, 1, 5] | ||||
|     expected = [0, 5] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_empty(): | ||||
|     values = [] | ||||
|     expected = [] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_plateau(): | ||||
|     values = [1, 2, 2, 1] | ||||
|     expected = [1] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_not_a_plateau(): | ||||
|     values = [1, 2, 2, 3, 1] | ||||
|     expected = [3] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| # the tests below here fail, can you get them to pass? | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_correct_order(): | ||||
|     values = [2, 1, 5, 1, 9] | ||||
|     expected = [0, 2, 4] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_one_value(): | ||||
|     values = [1] | ||||
|     expected = [0] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_long_plateau(): | ||||
|     values = [1, 2, 2, 2, 2, 2, 1, 8, 0] | ||||
|     expected = [3, 7] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_plateau_at_end(): | ||||
|     values = [1, 2, 2] | ||||
|     expected = [1] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_plateau_at_start(): | ||||
|     values = [1, 1, 0, 0] | ||||
|     expected = [0] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_all_same_values(): | ||||
|     values = [1, 1] | ||||
|     expected = [0] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_letters(): | ||||
|     values = ["T", "e", "s", "t", "s", "!"] | ||||
|     expected = [0, 3] | ||||
|     maxima = find_maxima(values) | ||||
|     assert maxima == expected | ||||
| 
 | ||||
| 
 | ||||
| def test_find_maxima_new_inputs_to_make_current_function_fail(): | ||||
|     # should you actually be done with all tests, then you can think of other cases where the current function fails | ||||
|     # and write tests for them and fix them | ||||
|     assert True | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue