Added stack challenge.

This commit is contained in:
Donne Martin
2015-07-02 23:06:52 -04:00
parent 8434c7d9f9
commit ba48e81bda
5 changed files with 305 additions and 13 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"
]
},
{
@@ -27,7 +34,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",
"* None"
]
@@ -130,13 +137,14 @@
],
"source": [
"%%writefile stack.py\n",
"\n",
"class Node(object):\n",
" \n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.next = None\n",
"\n",
"class Stack(object):\n",
" \n",
" def __init__(self, top=None):\n",
" self.top = top\n",
"\n",
@@ -177,8 +185,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"
]
},
{
@@ -192,17 +199,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Empty stack\n",
"Test: One element\n",
"Test: More than one element\n",
"Success: test_end_to_end\n"
"Overwriting test_stack.py\n"
]
}
],
"source": [
"%%writefile test_stack.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
"\n",
"class TestStack(object):\n",
" \n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
@@ -233,9 +240,34 @@
" \n",
" print('Success: test_end_to_end')\n",
"\n",
"def main():\n",
" test = TestStack()\n",
" test.test_end_to_end()\n",
" \n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_end_to_end()"
" main()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Empty stack\n",
"Test: One element\n",
"Test: More than one element\n",
"Success: test_end_to_end\n"
]
}
],
"source": [
"%run -i test_stack.py"
]
},
{