Added fibonacci challenge.

This commit is contained in:
Donne Martin
2015-07-01 06:48:04 -04:00
parent 3a44e384ef
commit 0b5acdb52f
3 changed files with 227 additions and 29 deletions

View File

@@ -16,9 +16,7 @@
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code: Recursive](#Code:-Recursive)\n",
"* [Code: Dynamic](#Code:-Dynamic)\n",
"* [Code: Iterative](#Code:-Iterative)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
@@ -39,9 +37,9 @@
"source": [
"## Test Cases\n",
"\n",
"* n = 0\n",
"* n = 1\n",
"* n > 1"
"* n = 0 -> 0\n",
"* n = 1 -> 0\n",
"* n > 1 -> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..."
]
},
{
@@ -56,14 +54,14 @@
"\n",
"Complexity:\n",
"* Time: O(2^n) if recursive or iterative, O(n) if dynamic\n",
"* Space: O(n) if recursive, O(1) if iterative, O(1) if dynamic"
"* Space: O(n) if recursive, O(1) if iterative, O(n) if dynamic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Recursive"
"## Code"
]
},
{
@@ -81,13 +79,6 @@
" return fib_recursive(n-1) + fib_recursive(n-2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Dynamic"
]
},
{
"cell_type": "code",
"execution_count": 2,
@@ -108,13 +99,6 @@
" return cache[n]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Iterative"
]
},
{
"cell_type": "code",
"execution_count": 3,
@@ -151,16 +135,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_fib\n",
"Success: test_fib\n",
"Success: test_fib\n"
"Overwriting test_fibonacci.py\n"
]
}
],
"source": [
"%%writefile test_fibonacci.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
"\n",
"class TestFib(object):\n",
" \n",
" def test_fib(self, func):\n",
" result = []\n",
" for i in xrange(num_items):\n",
@@ -169,11 +154,35 @@
" assert_equal(result, fib_seq)\n",
" print('Success: test_fib')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
"def main():\n",
" test = TestFib()\n",
" test.test_fib(fib_recursive)\n",
" test.test_fib(fib_dynamic)\n",
" test.test_fib(fib_iterative)"
" test.test_fib(fib_iterative)\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_fib\n",
"Success: test_fib\n",
"Success: test_fib\n"
]
}
],
"source": [
"%run -i test_fibonacci.py"
]
}
],