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

This commit is contained in:
Donne Martin 2015-06-27 06:33:14 -04:00
parent d6a8ff37e1
commit d3bd1fa290

View File

@ -13,19 +13,26 @@
"source": [ "source": [
"## Problem: Implement SetOfStacks that wraps a list of stacks, where each stack is bound by a capacity.\n", "## Problem: Implement SetOfStacks that wraps a list of stacks, where each stack is bound by a capacity.\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", "\n",
"* Can we assume we have a Stack class to use?\n", "*Problem statements are often intentionally 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",
"* If a stack becomes full, we should automatically create one?\n",
" * Yes\n",
"* If a stack becomes empty, should we delete it?\n",
" * Yes" " * Yes"
] ]
}, },
@ -81,7 +88,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": true "collapsed": true
}, },
@ -92,7 +99,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -147,29 +154,67 @@
" 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": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "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": [ "source": [
"print('Push on an empty stack')\n", "from nose.tools import assert_equal\n",
"capacity = 2\n", "\n",
"stacks = SetOfStacks(capacity)\n", "class Test(object):\n",
"stacks.push(3)\n", " def test_set_of_stacks(self):\n",
"print('Push on a non-empty stack')\n", " print('Test: Push on an empty stack')\n",
"stacks.push(5)\n", " capacity = 2\n",
"print('Push on a capacity stack to create a new one')\n", " stacks = SetOfStacks(capacity)\n",
"stacks.push('a')\n", " stacks.push(3)\n",
"print('Pop on a one element stack to destroy it')\n", "\n",
"print(stacks.pop())\n", " print('Test: Push on a non-empty stack')\n",
"print('Pop general case')\n", " stacks.push(5)\n",
"print(stacks.pop())\n", "\n",
"print(stacks.pop())\n", " print('Test: Push on a capacity stack to create a new one')\n",
"print('Pop on no elements')\n", " stacks.push('a')\n",
"print(stacks.pop())" "\n",
" print('Test: Pop on a one element stack to destroy it')\n",
" assert_equal(stacks.pop(), 'a')\n",
"\n",
" print('Test: Pop general case')\n",
" assert_equal(stacks.pop(), 5)\n",
" assert_equal(stacks.pop(), 3)\n",
"\n",
" print('Test: Pop on no elements')\n",
" assert_equal(stacks.pop(), None)\n",
" \n",
" print('Success: test_set_of_stacks')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_set_of_stacks()"
] ]
} }
], ],