From d3bd1fa290a3e7b0d659d8956e14735e7edc71e7 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 27 Jun 2015 06:33:14 -0400 Subject: [PATCH] Reworked notebook: Added more detail to constraints and test cases. Reworked unit test. --- stacks-queues/set-of-stacks.ipynb | 91 +++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 23 deletions(-) diff --git a/stacks-queues/set-of-stacks.ipynb b/stacks-queues/set-of-stacks.ipynb index 9e8d4b9..fedb9ec 100644 --- a/stacks-queues/set-of-stacks.ipynb +++ b/stacks-queues/set-of-stacks.ipynb @@ -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", - "capacity = 2\n", - "stacks = SetOfStacks(capacity)\n", - "stacks.push(3)\n", - "print('Push on a non-empty stack')\n", - "stacks.push(5)\n", - "print('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())" + "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", + "\n", + " print('Test: Push on a non-empty stack')\n", + " stacks.push(5)\n", + "\n", + " print('Test: Push on a capacity stack to create a new one')\n", + " stacks.push('a')\n", + "\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()" ] } ],