Added set of stacks challenge.

This commit is contained in:
Donne Martin
2015-07-02 23:05:48 -04:00
parent 1d9e3ad495
commit 9d5ef57818
4 changed files with 300 additions and 16 deletions

View File

@@ -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",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes\n",
@@ -90,11 +97,11 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"%run stack.py"
"%run ../stack/stack.py"
]
},
{
@@ -106,6 +113,7 @@
"outputs": [],
"source": [
"class StackWithCapacity(Stack):\n",
" \n",
" def __init__(self, top=None, capacity=10):\n",
" self.capacity = capacity\n",
" self.num_items = 0\n",
@@ -127,6 +135,7 @@
" return self.num_items == self.capacity\n",
"\n",
"class SetOfStacks(object):\n",
" \n",
" def __init__(self, capacity):\n",
" self.capacity = capacity\n",
" self.stacks = []\n",
@@ -159,8 +168,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"
]
},
{
@@ -174,20 +182,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Push on an empty stack\n",
"Test: Push on a non-empty stack\n",
"Test: Push on a capacity stack to create a new one\n",
"Test: Pop on a one element stack to destroy it\n",
"Test: Pop general case\n",
"Test: Pop on no elements\n",
"Success: test_set_of_stacks\n"
"Overwriting test_set_of_stacks.py\n"
]
}
],
"source": [
"%%writefile test_set_of_stacks.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
"\n",
"class TestSetOfStacks(object):\n",
" \n",
" def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\n",
" capacity = 2\n",
@@ -212,9 +217,37 @@
" \n",
" print('Success: test_set_of_stacks')\n",
"\n",
"def main():\n",
" test = TestSetOfStacks()\n",
" test.test_set_of_stacks()\n",
" \n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_set_of_stacks()"
" main()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Push on an empty stack\n",
"Test: Push on a non-empty stack\n",
"Test: Push on a capacity stack to create a new one\n",
"Test: Pop on a one element stack to destroy it\n",
"Test: Pop general case\n",
"Test: Pop on no elements\n",
"Success: test_set_of_stacks\n"
]
}
],
"source": [
"%run -i test_set_of_stacks.py"
]
}
],