From 5719b04c100432a4baee53f3f84055397e3c4ffa Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 27 Jun 2015 06:34:57 -0400 Subject: [PATCH] Reworked notebook: Added more detail to constraints and test cases. Reworked unit test. --- stacks-queues/stack-min.ipynb | 97 +++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 26 deletions(-) diff --git a/stacks-queues/stack-min.ipynb b/stacks-queues/stack-min.ipynb index e9e73a7..17f7284 100644 --- a/stacks-queues/stack-min.ipynb +++ b/stacks-queues/stack-min.ipynb @@ -13,19 +13,24 @@ "source": [ "## Problem: Implement a stack with push, pop, and min methods running O(1) time.\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", - "* Is this a stack of ints?\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 this is a stack of ints?\n", + " * Yes\n", + "* Can we assume we already have a stack class that can be used for this problem?\n", " * Yes" ] }, @@ -49,7 +54,7 @@ "\n", "### Min\n", "\n", - "* If the second stack is empty, return the max int value\n", + "* If the second stack is empty, return an error code (max int value)\n", "* Else, return the top of the stack, without popping it\n", "\n", "Complexity:\n", @@ -87,7 +92,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -98,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -129,31 +134,71 @@ " 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 empty stack, non-empty stack\n", + "Test: Pop on non-empty stack\n", + "Test: Pop empty stack\n", + "Success: test_stack_min\n" + ] + } + ], "source": [ - "print('Push on empty stack, non-empty stack')\n", - "stack = MyStack()\n", - "stack.push(5)\n", - "print('Push:', stack.peek(), 'Min after push:', stack.min())\n", - "stack.push(1)\n", - "print('Push:', stack.peek(), 'Min after push:', stack.min())\n", - "stack.push(3)\n", - "print('Push:', stack.peek(), 'Min after push:', stack.min())\n", - "stack.push(0)\n", - "print('Push:', stack.peek(), 'Min after push:', stack.min())\n", - "print('Pop on non-empty stack')\n", - "print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n", - "print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n", - "print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n", - "print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n", - "print('Stack contents:', stack.peek())\n", - "print('Pop empty stack:', stack.pop())" + "from nose.tools import assert_equal\n", + "\n", + "class Test(object):\n", + " def test_stack_min(self):\n", + " print('Test: Push on empty stack, non-empty stack')\n", + " stack = MyStack()\n", + " stack.push(5)\n", + " assert_equal(stack.peek(), 5)\n", + " assert_equal(stack.min(), 5)\n", + " stack.push(1)\n", + " assert_equal(stack.peek(), 1)\n", + " assert_equal(stack.min(), 1)\n", + " stack.push(3)\n", + " assert_equal(stack.peek(), 3)\n", + " assert_equal(stack.min(), 1)\n", + " stack.push(0)\n", + " assert_equal(stack.peek(), 0)\n", + " assert_equal(stack.min(), 0)\n", + "\n", + " print('Test: Pop on non-empty stack')\n", + " assert_equal(stack.pop(), 0)\n", + " assert_equal(stack.min(), 1)\n", + " assert_equal(stack.pop(), 3)\n", + " assert_equal(stack.min(), 1)\n", + " assert_equal(stack.pop(), 1)\n", + " assert_equal(stack.min(), 5)\n", + " assert_equal(stack.pop(), 5)\n", + " assert_equal(stack.min(), sys.maxint)\n", + "\n", + " print('Test: Pop empty stack')\n", + " assert_equal(stack.pop(), None)\n", + " \n", + " print('Success: test_stack_min')\n", + "\n", + "if __name__ == '__main__':\n", + " test = Test()\n", + " test.test_stack_min()" ] } ],