mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Reworked notebook: Added more detail to constraints and test cases. Reworked algorithm discussion and unit test.
This commit is contained in:
parent
78adc891be
commit
582e3c0181
|
@ -13,17 +13,20 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement n stacks using a single array.\n",
|
"## Problem: Implement n stacks using a single array.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)"
|
"* [Code](#Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Constraints and Assumptions\n",
|
||||||
|
"\n",
|
||||||
|
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Are the stacks and array a fixed size?\n",
|
"* Are the stacks and array a fixed size?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
|
@ -48,8 +51,13 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Initialize array\n",
|
"### Absolute Index\n",
|
||||||
"* Initialize stack pointers to -1\n",
|
"\n",
|
||||||
|
"* return stack size * stack index + stack pointer\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"* Time: O(1)\n",
|
||||||
|
"* Space: O(1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Push\n",
|
"### Push\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -74,15 +82,6 @@
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(1)\n",
|
"* Time: O(1)\n",
|
||||||
"* Space: O(1)\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"### Absolute Index\n",
|
|
||||||
"\n",
|
|
||||||
"* return stack size * stack index + stack pointer\n",
|
|
||||||
"\n",
|
|
||||||
"Complexity:\n",
|
|
||||||
"* Time: O(1)\n",
|
|
||||||
"* Space: O(1)"
|
"* Space: O(1)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -95,7 +94,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": true
|
||||||
},
|
},
|
||||||
|
@ -130,39 +129,77 @@
|
||||||
" return data"
|
" return data"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test\n",
|
||||||
|
"\n",
|
||||||
|
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Test: Pop on empty stack\n",
|
||||||
|
"Test: Push to full stack\n",
|
||||||
|
"Test: Push to non-full stack\n",
|
||||||
|
"Test: Pop on non-empty stack\n",
|
||||||
|
"Success: test_stacks\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"num_stacks = 3\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"stack_size = 100\n",
|
"from nose.tools import raises\n",
|
||||||
"print('Pop on empty stack')\n",
|
"\n",
|
||||||
|
"class Test(object):\n",
|
||||||
|
" @raises(Exception)\n",
|
||||||
|
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
||||||
|
" print('Test: Pop on empty stack')\n",
|
||||||
" stacks = Stacks(num_stacks, stack_size)\n",
|
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||||
"try:\n",
|
|
||||||
" stacks.pop(0)\n",
|
" stacks.pop(0)\n",
|
||||||
"except Exception as e:\n",
|
"\n",
|
||||||
" print(e)\n",
|
" @raises(Exception)\n",
|
||||||
"print('Push to non-full stack')\n",
|
" def test_push_on_full(self, num_stacks, stack_size):\n",
|
||||||
|
" print('Test: Push to full stack')\n",
|
||||||
|
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||||
|
" for i in xrange(0, stack_size):\n",
|
||||||
|
" stacks.push(2, i)\n",
|
||||||
|
" stacks.push(2, stack_size)\n",
|
||||||
|
"\n",
|
||||||
|
" def test_stacks(self, num_stacks, stack_size):\n",
|
||||||
|
" print('Test: Push to non-full stack')\n",
|
||||||
|
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||||
" stacks.push(0, 1)\n",
|
" stacks.push(0, 1)\n",
|
||||||
" stacks.push(0, 2)\n",
|
" stacks.push(0, 2)\n",
|
||||||
" stacks.push(1, 3)\n",
|
" stacks.push(1, 3)\n",
|
||||||
" stacks.push(2, 4)\n",
|
" stacks.push(2, 4)\n",
|
||||||
"print('Pop on non-empty stack')\n",
|
"\n",
|
||||||
"print(stacks.pop(0))\n",
|
" print('Test: Pop on non-empty stack')\n",
|
||||||
"print(stacks.pop(0))\n",
|
" assert_equal(stacks.pop(0), 2)\n",
|
||||||
"print(stacks.pop(1))\n",
|
" assert_equal(stacks.pop(0), 1)\n",
|
||||||
"print(stacks.pop(2))\n",
|
" assert_equal(stacks.pop(1), 3)\n",
|
||||||
"print('Push to full stack')\n",
|
" assert_equal(stacks.pop(2), 4)\n",
|
||||||
"for i in xrange(0, stack_size):\n",
|
"\n",
|
||||||
" stacks.push(2, i)\n",
|
" print('Success: test_stacks\\n')\n",
|
||||||
"try:\n",
|
"\n",
|
||||||
" stacks.push(2, stack_size)\n",
|
"if __name__ == '__main__':\n",
|
||||||
"except Exception as e:\n",
|
" num_stacks = 3\n",
|
||||||
" print(e)"
|
" stack_size = 100 \n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
||||||
|
" test.test_push_on_full(num_stacks, stack_size)\n",
|
||||||
|
" test.test_stacks(num_stacks, stack_size)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user