summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaidong Ji2018-08-16 22:18:18 -0500
committerHaidong Ji2018-08-16 22:18:18 -0500
commit18030db87580ab02962686509f65c7e1cfcd2d4c (patch)
tree9128a204152692e6b0782804017316af6fbf1b4f
parent2a6711ab6b09f5ab7bb066cd6c11670e6ff2bd90 (diff)
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!
-rw-r--r--AlgoDesignAndTechniqueEdxPython/.pydevproject5
-rw-r--r--AlgoDesignAndTechniqueEdxPython/sources/__init__.py0
-rw-r--r--AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py29
-rw-r--r--AlgoDesignAndTechniqueEdxPython/tests/__init__.py0
-rw-r--r--AlgoDesignAndTechniqueEdxPython/tests/fiblastdigitTest.py25
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()