mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-03-04 14:48:45 +00:00
Added n-stacks challenge.
This commit is contained in:
@@ -4,7 +4,14 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Solution Notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -26,7 +33,7 @@
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"\n",
|
||||
"* Are the stacks and array a fixed size?\n",
|
||||
" * Yes"
|
||||
@@ -39,9 +46,9 @@
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* Test the following on the three stacks:\n",
|
||||
" * Push to full stack\n",
|
||||
" * Push to full stack -> Exception\n",
|
||||
" * Push to non-full stack\n",
|
||||
" * Pop on empty stack\n",
|
||||
" * Pop on empty stack -> Exception\n",
|
||||
" * Pop on non-empty stack"
|
||||
]
|
||||
},
|
||||
@@ -101,6 +108,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Stacks(object):\n",
|
||||
" \n",
|
||||
" def __init__(self, num_stacks, stack_size):\n",
|
||||
" self.num_stacks = num_stacks\n",
|
||||
" self.stack_size = stack_size\n",
|
||||
@@ -134,8 +142,7 @@
|
||||
"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.*"
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -149,20 +156,18 @@
|
||||
"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"
|
||||
"Overwriting test_n_stacks.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_n_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import raises\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
"\n",
|
||||
"class TestStacks(object):\n",
|
||||
" \n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
||||
" print('Test: Pop on empty stack')\n",
|
||||
@@ -193,13 +198,40 @@
|
||||
"\n",
|
||||
" print('Success: test_stacks\\n')\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
"def main():\n",
|
||||
" num_stacks = 3\n",
|
||||
" stack_size = 100 \n",
|
||||
" test = Test()\n",
|
||||
" test = TestStacks()\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)"
|
||||
" test.test_stacks(num_stacks, stack_size)\n",
|
||||
" \n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"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": [
|
||||
"run -i test_n_stacks.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user