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": [
"## Problem: Implement SetOfStacks that wraps a list of stacks, where each stack is bound by a capacity.\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"## Constraints and Assumptions\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"
]
},
@ -81,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": true
},
@ -92,7 +99,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"collapsed": false
},
@ -147,29 +154,67 @@
" 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",
"execution_count": null,
"execution_count": 3,
"metadata": {
"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": [
"print('Push on an empty stack')\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\n",
" capacity = 2\n",
" stacks = SetOfStacks(capacity)\n",
" stacks.push(3)\n",
"print('Push on a non-empty stack')\n",
"\n",
" print('Test: Push on a non-empty stack')\n",
" stacks.push(5)\n",
"print('Push on a capacity stack to create a new one')\n",
"\n",
" print('Test: Push on a capacity stack to create a new one')\n",
" stacks.push('a')\n",
"print('Pop on a one element stack to destroy it')\n",
"print(stacks.pop())\n",
"print('Pop general case')\n",
"print(stacks.pop())\n",
"print(stacks.pop())\n",
"print('Pop on no elements')\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()"
]
}
],