Reworked notebook: Added more detail to constraints and test cases. Reworked unit test.

This commit is contained in:
Donne Martin
2015-06-27 06:35:21 -04:00
parent 5719b04c10
commit dd0e94fdd1

View File

@@ -13,10 +13,11 @@
"source": [ "source": [
"## Problem: Implement a stack with push, pop, peek, and is_empty methods using a linked list.\n", "## Problem: Implement a stack with push, pop, peek, and is_empty methods using a linked list.\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)\n", "* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Pythonic-Code](#Pythonic-Code)" "* [Pythonic-Code](#Pythonic-Code)"
] ]
}, },
@@ -24,7 +25,9 @@
"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",
"* None" "* None"
] ]
@@ -112,11 +115,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting stack.py\n"
]
}
],
"source": [ "source": [
"%%writefile stack.py\n", "%%writefile stack.py\n",
"\n", "\n",
@@ -152,7 +163,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@@ -161,36 +172,70 @@
"%run stack.py" "%run stack.py"
] ]
}, },
{
"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": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "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": [ "source": [
"print('Empty stack')\n", "from nose.tools import assert_equal\n",
"stack = Stack()\n", "\n",
"print(stack.peek())\n", "class Test(object):\n",
"print(stack.pop())\n", " # TODO: It would be better if we had unit tests for each\n",
"print('One element')\n", " # method in addition to the following end-to-end test\n",
"top = Node(5)\n", " def test_end_to_end(self):\n",
"stack = Stack(top)\n", " print('Test: Empty stack')\n",
"print(stack.pop())\n", " stack = Stack()\n",
"print(stack.peek())\n", " assert_equal(stack.peek(), None)\n",
"print('More than one element')\n", " assert_equal(stack.pop(), None)\n",
"stack = Stack()\n", "\n",
"stack.push(1)\n", " print('Test: One element')\n",
"stack.push(2)\n", " top = Node(5)\n",
"stack.push(3)\n", " stack = Stack(top)\n",
"print(stack.pop())\n", " assert_equal(stack.pop(), 5)\n",
"print(stack.peek())\n", " assert_equal(stack.peek(), None)\n",
"print(stack.pop())\n", "\n",
"print(stack.peek())\n", " print('Test: More than one element')\n",
"print(stack.is_empty())\n", " stack = Stack()\n",
"print(stack.pop())\n", " stack.push(1)\n",
"print(stack.peek())\n", " stack.push(2)\n",
"print(stack.is_empty())" " stack.push(3)\n",
" assert_equal(stack.pop(), 3)\n",
" assert_equal(stack.peek(), 2)\n",
" assert_equal(stack.pop(), 2)\n",
" assert_equal(stack.peek(), 1)\n",
" assert_equal(stack.is_empty(), False)\n",
" assert_equal(stack.pop(), 1)\n",
" assert_equal(stack.peek(), None)\n",
" assert_equal(stack.is_empty(), True)\n",
" \n",
" print('Success: test_end_to_end')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_end_to_end()"
] ]
}, },
{ {