Polish fibonacci challenge and solution (#91)

Update constraints, test cases, tests, and code.
This commit is contained in:
Donne Martin 2016-07-11 00:12:10 -04:00 committed by GitHub
parent cb660acd13
commit a868f1e597
3 changed files with 59 additions and 46 deletions

View File

@ -34,7 +34,14 @@
"source": [
"## Constraints\n",
"\n",
"* None"
"* Does the sequence start at 0 or 1?\n",
" * 0\n",
"* Can we assume the inputs are valid non-negative ints?\n",
" * Yes\n",
"* Are you looking for a recursive or iterative solution?\n",
" * Implement both\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
@ -45,7 +52,8 @@
"\n",
"* n = 0 -> 0\n",
"* n = 1 -> 1\n",
"* n > 1 -> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..."
"* n = 6 -> 8\n",
"* Fib sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..."
]
},
{
@ -85,11 +93,7 @@
},
"outputs": [],
"source": [
"num_items = 10\n",
"cache = [None] * (num_items + 1)\n",
"\n",
"\n",
"def fib_dynamic(n):\n",
"def fib_iterative(n):\n",
" # TODO: Implement me\n",
" pass"
]
@ -102,7 +106,7 @@
},
"outputs": [],
"source": [
"def fib_iterative(n):\n",
"def fib_dynamic(n):\n",
" # TODO: Implement me\n",
" pass"
]
@ -134,10 +138,10 @@
"\n",
" def test_fib(self, func):\n",
" result = []\n",
" for i in range(num_items):\n",
" expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
" for i in range(len(expected)):\n",
" result.append(func(i))\n",
" fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
" assert_equal(result, fib_seq)\n",
" assert_equal(result, expected)\n",
" print('Success: test_fib')\n",
"\n",
"\n",
@ -164,21 +168,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,

View File

@ -33,7 +33,14 @@
"source": [
"## Constraints\n",
"\n",
"* None"
"* Does the sequence start at 0 or 1?\n",
" * 0\n",
"* Can we assume the inputs are valid non-negative ints?\n",
" * Yes\n",
"* Are you looking for a recursive or iterative solution?\n",
" * Either\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
@ -44,7 +51,8 @@
"\n",
"* n = 0 -> 0\n",
"* n = 1 -> 1\n",
"* n > 1 -> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..."
"* n = 6 -> 8\n",
"* Fib sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..."
]
},
{
@ -93,17 +101,12 @@
},
"outputs": [],
"source": [
"num_items = 10\n",
"cache = [None] * (num_items + 1)\n",
"\n",
"\n",
"def fib_dynamic(n):\n",
" if n == 0 or n == 1:\n",
" return n\n",
" if cache[n] != None:\n",
" return cache[n]\n",
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
" return cache[n]"
"def fib_iterative(n):\n",
" a = 0\n",
" b = 1\n",
" for _ in range(n):\n",
" a, b = b, a + b\n",
" return a"
]
},
{
@ -114,12 +117,18 @@
},
"outputs": [],
"source": [
"def fib_iterative(n):\n",
" a = 0\n",
" b = 1\n",
" for _ in range(n):\n",
" a, b = b, a + b\n",
" return a"
"def fib_dynamic(n):\n",
" cache = {}\n",
" return _fib_dynamic(n, cache)\n",
"\n",
"\n",
"def _fib_dynamic(n, cache):\n",
" if n == 0 or n == 1:\n",
" return n\n",
" if n in cache:\n",
" return cache[n]\n",
" cache[n] = _fib_dynamic(n-1, cache) + _fib_dynamic(n-2, cache)\n",
" return cache[n]"
]
},
{
@ -154,10 +163,10 @@
"\n",
" def test_fib(self, func):\n",
" result = []\n",
" for i in range(num_items):\n",
" expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
" for i in range(len(expected)):\n",
" result.append(func(i))\n",
" fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
" assert_equal(result, fib_seq)\n",
" assert_equal(result, expected)\n",
" print('Success: test_fib')\n",
"\n",
"\n",
@ -196,21 +205,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,

View File

@ -5,10 +5,10 @@ class TestFib(object):
def test_fib(self, func):
result = []
for i in range(num_items):
expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
for i in range(len(expected)):
result.append(func(i))
fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
assert_equal(result, fib_seq)
assert_equal(result, expected)
print('Success: test_fib')