Last active
August 29, 2015 13:58
-
-
Save yeukhon/9960733 to your computer and use it in GitHub Desktop.
local_min.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def find_local_min(A): | |
if len(A) == 0: | |
return None | |
elif len(A) == 1: | |
return A[0] | |
else: | |
if A[0] < A[1]: | |
return A[0] | |
for i in xrange(1, len(A)): | |
if A[i-1] > A[i]: | |
if i+1 < len(A): | |
if A[i+1] > A[i]: | |
return A[i] | |
else: | |
return A[i] | |
class TestMin(unittest.TestCase): | |
def test_33(self): | |
self.assertEqual(find_local_min([3,3]),None) | |
def test_123(self): | |
self.assertEqual(find_local_min([1,2,3]), 1) | |
def test_323(self): | |
self.assertEqual(find_local_min([3,2,3]), 2) | |
def test_334561(self): | |
self.assertEqual(find_local_min([3,3,4,5,6,1]), 1) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment