From 0c43a34ae17d0533faeaa3476e8ca84dca7126db Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 27 Jun 2015 06:36:52 -0400 Subject: [PATCH] Reworked notebook: Added more detail to constraints and test cases. Reworked unit test. Added linebreak to comment about naming clash with queue. --- stacks-queues/queue-list.ipynb | 97 ++++++++++++++++++++++++++-------- stacks-queues/queue_list.py | 3 +- 2 files changed, 76 insertions(+), 24 deletions(-) diff --git a/stacks-queues/queue-list.ipynb b/stacks-queues/queue-list.ipynb index ea87447..5b91978 100644 --- a/stacks-queues/queue-list.ipynb +++ b/stacks-queues/queue-list.ipynb @@ -13,10 +13,11 @@ "source": [ "## Problem: Implement a queue with enqueue and dequeue methods using a linked list.\n", "\n", - "* [Clarifying Questions](#Clarifying-Questions)\n", + "* [Constraints and Assumptions](#Constraints-and-Assumptions)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", "* [Pythonic-Code](#Pythonic-Code)" ] }, @@ -24,7 +25,9 @@ "cell_type": "markdown", "metadata": {}, "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", "* If there is one item in the list, do you expect the first and last pointers to both point to it?\n", " * Yes\n", @@ -91,14 +94,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting queue_list.py\n" + ] + } + ], "source": [ "%%writefile queue_list.py\n", - "# Need to avoid naming this queue.py as it will conflict with IPython Notebook\n", + "# Need to avoid naming this queue.py as it \n", + "# will conflict with IPython Notebook\n", "\n", "class Node(object):\n", " def __init__(self, data):\n", @@ -123,6 +135,7 @@ " # Empty list\n", " if self.first is None and self.last is None:\n", " return None\n", + " \n", " # Remove only element from a one element list\n", " elif self.first == self.last:\n", " data = self.first.data\n", @@ -137,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -146,29 +159,67 @@ "%run queue_list.py" ] }, + { + "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: Dequeue an empty queue\n", + "Test: Enqueue to an empty queue\n", + "Test: Dequeue a queue with one element\n", + "Test: Enqueue to a non-empty queue\n", + "Test: Dequeue a queue with more than one element\n", + "Success: test_end_to_end\n" + ] + } + ], "source": [ - "print('Dequeue an empty queue')\n", - "queue = Queue()\n", - "print(queue.dequeue())\n", - "print('Enqueue to an empty queue')\n", - "queue.enqueue(1)\n", - "print('Dequeue a queue with one element')\n", - "print(queue.dequeue())\n", - "print('Enqueue to a non-empty queue')\n", - "queue.enqueue(2)\n", - "queue.enqueue(3)\n", - "queue.enqueue(4)\n", - "print('Dequeue a queue with more than one element')\n", - "print(queue.dequeue())\n", - "print(queue.dequeue())\n", - "print(queue.dequeue())" + "from nose.tools import assert_equal\n", + "\n", + "class Test(object):\n", + " # TODO: It would be better if we had unit tests for each\n", + " # method in addition to the following end-to-end test\n", + " def test_end_to_end(self):\n", + " print('Test: Dequeue an empty queue')\n", + " queue = Queue()\n", + " assert_equal(queue.dequeue(), None)\n", + "\n", + " print('Test: Enqueue to an empty queue')\n", + " queue.enqueue(1)\n", + "\n", + " print('Test: Dequeue a queue with one element')\n", + " assert_equal(queue.dequeue(), 1)\n", + "\n", + " print('Test: Enqueue to a non-empty queue')\n", + " queue.enqueue(2)\n", + " queue.enqueue(3)\n", + " queue.enqueue(4)\n", + "\n", + " print('Test: Dequeue a queue with more than one element')\n", + " assert_equal(queue.dequeue(), 2)\n", + " assert_equal(queue.dequeue(), 3)\n", + " assert_equal(queue.dequeue(), 4)\n", + " \n", + " print('Success: test_end_to_end')\n", + "\n", + "if __name__ == '__main__':\n", + " test = Test()\n", + " test.test_end_to_end()" ] }, { diff --git a/stacks-queues/queue_list.py b/stacks-queues/queue_list.py index 25b7262..c73339f 100644 --- a/stacks-queues/queue_list.py +++ b/stacks-queues/queue_list.py @@ -1,4 +1,5 @@ -# Need to avoid naming this queue.py as it will conflict with IPython Notebook +# Need to avoid naming this queue.py as it +# will conflict with IPython Notebook class Node(object): def __init__(self, data):