diff options
5 files changed, 58 insertions, 1 deletions
diff --git a/AlgoDesignAndTechniqueEdxPython/.pydevproject b/AlgoDesignAndTechniqueEdxPython/.pydevproject index ab4689e..d56b5ad 100644 --- a/AlgoDesignAndTechniqueEdxPython/.pydevproject +++ b/AlgoDesignAndTechniqueEdxPython/.pydevproject @@ -1,10 +1,13 @@ <?xml version="1.0" encoding="UTF-8" standalone="no"?> <?eclipse-pydev version="1.0"?><pydev_project> + <pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH"> <path>/${PROJECT_DIR_NAME}</path> - <path>/${PROJECT_DIR_NAME}/sources</path> <path>/${PROJECT_DIR_NAME}/tests</path> </pydev_pathproperty> + <pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property> + <pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property> + </pydev_project> diff --git a/AlgoDesignAndTechniqueEdxPython/sources/__init__.py b/AlgoDesignAndTechniqueEdxPython/sources/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/__init__.py diff --git a/AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py b/AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py new file mode 100644 index 0000000..cca9cd9 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py @@ -0,0 +1,29 @@ +# Uses python3 +import sys +def getFibLastDigitNaive(n): + if n <= 1: + return n + + previous = 0 + current = 1 + + for _ in range(n - 1): + previous, current = current, previous + current + + return current % 10 + + +def getFibLastDigitOptimized(n): + if (n <= 1): + return n + fibLst = [None] * (n+1) + fibLst[0] = 0 + fibLst[1] = 1 + for i in range(2, n+1): + fibLst[i] = (fibLst[i-1] + fibLst[i-2]) % 10 + return fibLst[n] + +if __name__ == '__main__': + input = sys.stdin.read() + n = int(input) + print(getFibLastDigitOptimized(n)) diff --git a/AlgoDesignAndTechniqueEdxPython/tests/__init__.py b/AlgoDesignAndTechniqueEdxPython/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/__init__.py diff --git a/AlgoDesignAndTechniqueEdxPython/tests/fiblastdigitTest.py b/AlgoDesignAndTechniqueEdxPython/tests/fiblastdigitTest.py new file mode 100644 index 0000000..040197b --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/fiblastdigitTest.py @@ -0,0 +1,25 @@ +''' +Created on Aug 16, 2018 + +@author: haidong +''' +import unittest + +from sources.fiblastdigit import getFibLastDigitNaive + + +class Test(unittest.TestCase): + + + def testName(self): + self.assertEqual(0, getFibLastDigitNaive(0)) + self.assertEqual(1, getFibLastDigitNaive(1)) + self.assertEqual(2, getFibLastDigitNaive(3)) + self.assertEqual(5, getFibLastDigitNaive(40)) + self.assertEqual(9, getFibLastDigitNaive(331)) + self.assertEqual(5, getFibLastDigitNaive(327305)) + + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + unittest.main() |