Cleaned up code.

This commit is contained in:
Donne Martin 2015-05-30 11:06:39 -04:00
parent b34be263cc
commit bd87a17326

View File

@ -80,7 +80,7 @@
" else:\n",
" return fib_recursive(n-1) + fib_recursive(n-2)\n",
"\n",
"for i in xrange(0, num_items):\n",
"for i in xrange(num_items):\n",
" print(fib_recursive(i))"
]
},
@ -102,7 +102,7 @@
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
" return cache[n]\n",
"\n",
"for i in xrange(0, num_items):\n",
"for i in xrange(num_items):\n",
" print(fib_dynamic(i))"
]
},
@ -115,18 +115,13 @@
"outputs": [],
"source": [
"def fib_iterative(n):\n",
" if n == 0 or n == 1:\n",
" return n\n",
" else:\n",
" n_minus_2 = 0 \n",
" n_minus_1 = 1\n",
" total = 0\n",
" for _ in xrange(2, n+1):\n",
" total = n_minus_2 + n_minus_1\n",
" n_minus_2, n_minus_1 = n_minus_1, total\n",
" return total\n",
" a = 0 \n",
" b = 1\n",
" for _ in xrange(n):\n",
" a, b = b, a + b\n",
" return a\n",
"\n",
"for i in xrange(0, num_items):\n",
"for i in xrange(num_items):\n",
" print(fib_iterative(i))"
]
}