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

This commit is contained in:
Donne Martin 2015-06-27 06:34:09 -04:00
parent d3bd1fa290
commit 8c93b51fc6

View File

@ -13,21 +13,26 @@
"source": [ "source": [
"## Problem: Sort a stack. You can use another stack as a buffer.\n", "## Problem: Sort a stack. You can use another stack as a buffer.\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",
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n", "\n",
"* When sorted, should the largest element be at the top or bottom?\n", "* When sorted, should the largest element be at the top or bottom?\n",
" * Top\n", " * Top\n",
"* Can you have duplicate values, saw two 5s?\n", "* Can you have duplicate values like 5, 5?\n",
" * Yes\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes" " * Yes"
] ]
}, },
@ -71,7 +76,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": true "collapsed": true
}, },
@ -82,7 +87,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -99,32 +104,68 @@
" return buff" " return buff"
] ]
}, },
{
"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 stack\n",
"Test: Two or more element stack (general case)\n",
"Success: test_sort_stack\n"
]
}
],
"source": [ "source": [
"from random import randint\n", "from random import randint\n",
"from nose.tools import assert_equal\n",
"\n", "\n",
"def test_sort(num_items, stack=None):\n", "class Test(object):\n",
" if stack is None:\n", " def get_sorted_stack(self, numbers):\n",
" stack = MyStack()\n", " stack = MyStack()\n",
" for _ in xrange(0, num_items):\n", " for x in numbers:\n",
" stack.push(randint(0, num_items))\n", " stack.push(x)\n",
" sorted_stack = stack.sort()\n", " sorted_stack = stack.sort()\n",
" for _ in xrange(0, num_items):\n", " return sorted_stack\n",
" print(sorted_stack.pop())\n",
" return sorted_stack\n",
"\n", "\n",
"print('Empty stack')\n", " def test_sort_stack(self):\n",
"test_sort(0)\n", " print('Test: Empty stack')\n",
"print('One element stack')\n", " sorted_stack = self.get_sorted_stack([])\n",
"test_sort(1)\n", " assert_equal(sorted_stack.pop(), None)\n",
"print('Two or more element stack (general case)')\n", "\n",
"sorted_stack = test_sort(5)" " print('Test: One element stack')\n",
" sorted_stack = self.get_sorted_stack([1])\n",
" assert_equal(sorted_stack.pop(), 1)\n",
"\n",
" print('Test: Two or more element stack (general case)')\n",
" num_items = 10\n",
" numbers = [randint(0, 10) for x in xrange(num_items)]\n",
" sorted_stack = self.get_sorted_stack(numbers)\n",
" sorted_numbers = []\n",
" for _ in xrange(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n",
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
" \n",
" print('Success: test_sort_stack')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_sort_stack()"
] ]
} }
], ],