mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Cleaned up code.
This commit is contained in:
parent
b34be263cc
commit
bd87a17326
|
@ -80,7 +80,7 @@
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" return fib_recursive(n-1) + fib_recursive(n-2)\n",
|
" return fib_recursive(n-1) + fib_recursive(n-2)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for i in xrange(0, num_items):\n",
|
"for i in xrange(num_items):\n",
|
||||||
" print(fib_recursive(i))"
|
" print(fib_recursive(i))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -102,7 +102,7 @@
|
||||||
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
|
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
|
||||||
" return cache[n]\n",
|
" return cache[n]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for i in xrange(0, num_items):\n",
|
"for i in xrange(num_items):\n",
|
||||||
" print(fib_dynamic(i))"
|
" print(fib_dynamic(i))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -115,18 +115,13 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def fib_iterative(n):\n",
|
"def fib_iterative(n):\n",
|
||||||
" if n == 0 or n == 1:\n",
|
" a = 0 \n",
|
||||||
" return n\n",
|
" b = 1\n",
|
||||||
" else:\n",
|
" for _ in xrange(n):\n",
|
||||||
" n_minus_2 = 0 \n",
|
" a, b = b, a + b\n",
|
||||||
" n_minus_1 = 1\n",
|
" return a\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",
|
|
||||||
"\n",
|
"\n",
|
||||||
"for i in xrange(0, num_items):\n",
|
"for i in xrange(num_items):\n",
|
||||||
" print(fib_iterative(i))"
|
" print(fib_iterative(i))"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user