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,37 +76,21 @@
"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",
"\n",
" def fib_iterative(self, n):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass\n",
] "\n",
}, " def fib_recursive(self, n):\n",
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def fib_iterative(n):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass\n",
] "\n",
}, " def fib_dynamic(self, n):\n",
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def fib_dynamic(n):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " 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,52 +82,35 @@
"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", "\n",
" return n\n", " def fib_iterative(self, 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", " a = 0\n",
" b = 1\n", " b = 1\n",
" for _ in range(n):\n", " for _ in range(n):\n",
" a, b = b, a + b\n", " a, b = b, a + b\n",
" return a" " return a\n",
] "\n",
}, " def fib_recursive(self, n):\n",
{ " if n == 0 or n == 1:\n",
"cell_type": "code", " return n\n",
"execution_count": 3, " else:\n",
"metadata": { " return self.fib_recursive(n-1) + self.fib_recursive(n-2)\n",
"collapsed": false "\n",
}, " def fib_dynamic(self, n):\n",
"outputs": [],
"source": [
"def fib_dynamic(n):\n",
" cache = {}\n", " cache = {}\n",
" return _fib_dynamic(n, cache)\n", " return self._fib_dynamic(n, cache)\n",
"\n", "\n",
"\n", " def _fib_dynamic(self, n, cache):\n",
"def _fib_dynamic(n, cache):\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", " if n in cache:\n",
" return cache[n]\n", " return cache[n]\n",
" cache[n] = _fib_dynamic(n-1, cache) + _fib_dynamic(n-2, cache)\n", " cache[n] = self._fib_dynamic(n-1, cache) + self._fib_dynamic(n-2, cache)\n",
" return 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__':