From 18030db87580ab02962686509f65c7e1cfcd2d4c Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Thu, 16 Aug 2018 22:18:18 -0500 Subject: Fib last digit done. Yay! Bonus: figured PyDev TDD code generation issue. I created "sources" as a source folder, but really should have created as a package, for TDD code generation to work properly. Fixed now!--- AlgoDesignAndTechniqueEdxPython/.pydevproject | 5 +++- .../sources/__init__.py | 0 .../sources/fiblastdigit.py | 29 ++++++++++++++++++++++ AlgoDesignAndTechniqueEdxPython/tests/__init__.py | 0 .../tests/fiblastdigitTest.py | 25 +++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 AlgoDesignAndTechniqueEdxPython/sources/__init__.py create mode 100644 AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py create mode 100644 AlgoDesignAndTechniqueEdxPython/tests/__init__.py create mode 100644 AlgoDesignAndTechniqueEdxPython/tests/fiblastdigitTest.py diff --git a/AlgoDesignAndTechniqueEdxPython/.pydevproject b/AlgoDesignAndTechniqueEdxPython/.pydevproject index ab4689e..d56b5ad 100644 --- a/AlgoDesignAndTechniqueEdxPython/.pydevproject +++ b/AlgoDesignAndTechniqueEdxPython/.pydevproject @@ -1,10 +1,13 @@ + /${PROJECT_DIR_NAME} - /${PROJECT_DIR_NAME}/sources /${PROJECT_DIR_NAME}/tests + python interpreter + Default + diff --git a/AlgoDesignAndTechniqueEdxPython/sources/__init__.py b/AlgoDesignAndTechniqueEdxPython/sources/__init__.py new file mode 100644 index 0000000..e69de29 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 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() -- cgit v1.2.3