mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move Fibonacci challenge code to a class (#140)
This commit is contained in:
parent
f71936199b
commit
91b3efc3e4
|
@ -76,39 +76,23 @@
|
|||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fib_recursive(n):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fib_iterative(n):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fib_dynamic(n):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
"class Math(object):\n",
|
||||
"\n",
|
||||
" def fib_iterative(self, n):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def fib_recursive(self, n):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def fib_dynamic(self, n):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -147,9 +131,10 @@
|
|||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestFib()\n",
|
||||
" test.test_fib(fib_recursive)\n",
|
||||
" test.test_fib(fib_dynamic)\n",
|
||||
" test.test_fib(fib_iterative)\n",
|
||||
" math = Math()\n",
|
||||
" test.test_fib(math.fib_recursive)\n",
|
||||
" test.test_fib(math.fib_dynamic)\n",
|
||||
" test.test_fib(math.fib_iterative)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
|
|
|
@ -82,53 +82,36 @@
|
|||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fib_recursive(n):\n",
|
||||
" if n == 0 or n == 1:\n",
|
||||
" return n\n",
|
||||
" else:\n",
|
||||
" return fib_recursive(n-1) + fib_recursive(n-2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fib_iterative(n):\n",
|
||||
" a = 0\n",
|
||||
" b = 1\n",
|
||||
" for _ in range(n):\n",
|
||||
" a, b = b, a + b\n",
|
||||
" return a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fib_dynamic(n):\n",
|
||||
" cache = {}\n",
|
||||
" return _fib_dynamic(n, cache)\n",
|
||||
"class Math(object):\n",
|
||||
"\n",
|
||||
" def fib_iterative(self, n):\n",
|
||||
" a = 0\n",
|
||||
" b = 1\n",
|
||||
" for _ in range(n):\n",
|
||||
" a, b = b, a + b\n",
|
||||
" return a\n",
|
||||
"\n",
|
||||
"def _fib_dynamic(n, cache):\n",
|
||||
" if n == 0 or n == 1:\n",
|
||||
" return n\n",
|
||||
" if n in cache:\n",
|
||||
" return cache[n]\n",
|
||||
" cache[n] = _fib_dynamic(n-1, cache) + _fib_dynamic(n-2, cache)\n",
|
||||
" return cache[n]"
|
||||
" def fib_recursive(self, n):\n",
|
||||
" if n == 0 or n == 1:\n",
|
||||
" return n\n",
|
||||
" else:\n",
|
||||
" return self.fib_recursive(n-1) + self.fib_recursive(n-2)\n",
|
||||
"\n",
|
||||
" def fib_dynamic(self, n):\n",
|
||||
" cache = {}\n",
|
||||
" return self._fib_dynamic(n, cache)\n",
|
||||
"\n",
|
||||
" def _fib_dynamic(self, n, cache):\n",
|
||||
" if n == 0 or n == 1:\n",
|
||||
" return n\n",
|
||||
" if n in cache:\n",
|
||||
" return cache[n]\n",
|
||||
" cache[n] = self._fib_dynamic(n-1, cache) + self._fib_dynamic(n-2, cache)\n",
|
||||
" return cache[n]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -141,7 +124,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -172,9 +155,10 @@
|
|||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestFib()\n",
|
||||
" test.test_fib(fib_recursive)\n",
|
||||
" test.test_fib(fib_dynamic)\n",
|
||||
" test.test_fib(fib_iterative)\n",
|
||||
" math = Math()\n",
|
||||
" test.test_fib(math.fib_recursive)\n",
|
||||
" test.test_fib(math.fib_dynamic)\n",
|
||||
" test.test_fib(math.fib_iterative)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
|
@ -183,7 +167,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
|
|
@ -14,9 +14,10 @@ class TestFib(object):
|
|||
|
||||
def main():
|
||||
test = TestFib()
|
||||
test.test_fib(fib_recursive)
|
||||
test.test_fib(fib_dynamic)
|
||||
test.test_fib(fib_iterative)
|
||||
math = Math()
|
||||
test.test_fib(math.fib_recursive)
|
||||
test.test_fib(math.fib_dynamic)
|
||||
test.test_fib(math.fib_iterative)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user