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",
|
||||
" 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))"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user