Move Fibonacci challenge code to a class (#140)

This commit is contained in:
Donne Martin 2017-01-22 12:06:58 -05:00 committed by GitHub
parent f71936199b
commit 91b3efc3e4
3 changed files with 53 additions and 83 deletions

View File

@ -76,39 +76,23 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": true
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def fib_recursive(n):\n", "class Math(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def fib_iterative(self, n):\n",
] " # TODO: Implement me\n",
}, " pass\n",
{ "\n",
"cell_type": "code", " def fib_recursive(self, n):\n",
"execution_count": null, " # TODO: Implement me\n",
"metadata": { " pass\n",
"collapsed": false "\n",
}, " def fib_dynamic(self, n):\n",
"outputs": [], " # TODO: Implement me\n",
"source": [ " pass"
"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"
] ]
}, },
{ {
@ -147,9 +131,10 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestFib()\n", " test = TestFib()\n",
" test.test_fib(fib_recursive)\n", " math = Math()\n",
" test.test_fib(fib_dynamic)\n", " test.test_fib(math.fib_recursive)\n",
" test.test_fib(fib_iterative)\n", " test.test_fib(math.fib_dynamic)\n",
" test.test_fib(math.fib_iterative)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@ -82,53 +82,36 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": true
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def fib_recursive(n):\n", "class Math(object):\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",
"\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", "\n",
"def _fib_dynamic(n, cache):\n", " def fib_recursive(self, n):\n",
" if n == 0 or n == 1:\n", " if n == 0 or n == 1:\n",
" return n\n", " return n\n",
" if n in cache:\n", " else:\n",
" return cache[n]\n", " return self.fib_recursive(n-1) + self.fib_recursive(n-2)\n",
" cache[n] = _fib_dynamic(n-1, cache) + _fib_dynamic(n-2, cache)\n", "\n",
" return cache[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", "cell_type": "code",
"execution_count": 4, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -172,9 +155,10 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestFib()\n", " test = TestFib()\n",
" test.test_fib(fib_recursive)\n", " math = Math()\n",
" test.test_fib(fib_dynamic)\n", " test.test_fib(math.fib_recursive)\n",
" test.test_fib(fib_iterative)\n", " test.test_fib(math.fib_dynamic)\n",
" test.test_fib(math.fib_iterative)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@ -183,7 +167,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },

View File

@ -14,9 +14,10 @@ class TestFib(object):
def main(): def main():
test = TestFib() test = TestFib()
test.test_fib(fib_recursive) math = Math()
test.test_fib(fib_dynamic) test.test_fib(math.fib_recursive)
test.test_fib(fib_iterative) test.test_fib(math.fib_dynamic)
test.test_fib(math.fib_iterative)
if __name__ == '__main__': if __name__ == '__main__':