mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-20 00:08:02 +00:00
Polish fibonacci challenge and solution (#91)
Update constraints, test cases, tests, and code.
This commit is contained in:
@@ -34,7 +34,14 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\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",
|
||||||
"* n = 0 -> 0\n",
|
"* n = 0 -> 0\n",
|
||||||
"* n = 1 -> 1\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": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"num_items = 10\n",
|
"def fib_iterative(n):\n",
|
||||||
"cache = [None] * (num_items + 1)\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def fib_dynamic(n):\n",
|
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
@@ -102,7 +106,7 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def fib_iterative(n):\n",
|
"def fib_dynamic(n):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
@@ -134,10 +138,10 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def test_fib(self, func):\n",
|
" def test_fib(self, func):\n",
|
||||||
" result = []\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",
|
" result.append(func(i))\n",
|
||||||
" fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
|
" assert_equal(result, expected)\n",
|
||||||
" assert_equal(result, fib_seq)\n",
|
|
||||||
" print('Success: test_fib')\n",
|
" print('Success: test_fib')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -164,21 +168,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -33,7 +33,14 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\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",
|
||||||
"* n = 0 -> 0\n",
|
"* n = 0 -> 0\n",
|
||||||
"* n = 1 -> 1\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": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"num_items = 10\n",
|
"def fib_iterative(n):\n",
|
||||||
"cache = [None] * (num_items + 1)\n",
|
" a = 0\n",
|
||||||
"\n",
|
" b = 1\n",
|
||||||
"\n",
|
" for _ in range(n):\n",
|
||||||
"def fib_dynamic(n):\n",
|
" a, b = b, a + b\n",
|
||||||
" if n == 0 or n == 1:\n",
|
" return a"
|
||||||
" 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]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -114,12 +117,18 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def fib_iterative(n):\n",
|
"def fib_dynamic(n):\n",
|
||||||
" a = 0\n",
|
" cache = {}\n",
|
||||||
" b = 1\n",
|
" return _fib_dynamic(n, cache)\n",
|
||||||
" for _ in range(n):\n",
|
"\n",
|
||||||
" a, b = b, a + b\n",
|
"\n",
|
||||||
" return a"
|
"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",
|
"\n",
|
||||||
" def test_fib(self, func):\n",
|
" def test_fib(self, func):\n",
|
||||||
" result = []\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",
|
" result.append(func(i))\n",
|
||||||
" fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
|
" assert_equal(result, expected)\n",
|
||||||
" assert_equal(result, fib_seq)\n",
|
|
||||||
" print('Success: test_fib')\n",
|
" print('Success: test_fib')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -196,21 +205,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ class TestFib(object):
|
|||||||
|
|
||||||
def test_fib(self, func):
|
def test_fib(self, func):
|
||||||
result = []
|
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))
|
result.append(func(i))
|
||||||
fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
|
assert_equal(result, expected)
|
||||||
assert_equal(result, fib_seq)
|
|
||||||
print('Success: test_fib')
|
print('Success: test_fib')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user