From fea843789b23c3349170d23f2cba90e0ef61b13f Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 27 Jun 2015 06:39:33 -0400 Subject: [PATCH] Reworked notebook: Added more detail to constraints and test cases. Reworked code and unit test. --- sorting-searching/quick-sort.ipynb | 97 ++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 26 deletions(-) diff --git a/sorting-searching/quick-sort.ipynb b/sorting-searching/quick-sort.ipynb index 5103da9..5eecdc6 100644 --- a/sorting-searching/quick-sort.ipynb +++ b/sorting-searching/quick-sort.ipynb @@ -13,10 +13,11 @@ "source": [ "## Problem: Implement quick sort.\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,9 +25,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Clarifying Questions\n", + "## Constraints and Assumptions\n", "\n", - "* Is a naiive quicksort (ie not in-place) ok?\n", + "*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", + "\n", + "* Are you looking for a naiive solution (ie not in-place)?\n", " * Yes" ] }, @@ -73,7 +76,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -98,25 +101,6 @@ " return left + [pivot_value] + right" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print('Empty input')\n", - "data = []\n", - "print(quick_sort(data))\n", - "print('One element')\n", - "data = [5]\n", - "print(quick_sort(data))\n", - "print('Two or more elements')\n", - "data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", - "print(quick_sort(data))" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -128,17 +112,78 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "def quick_sort(arr): \n", + "def quick_sort_alt(arr): \n", " if len(arr) <= 1:\n", " return arr\n", " else:\n", - " return qsort([x for x in arr[1:] if x=arr[0]])" + " return quick_sort_alt([x for x in arr[1:] if x=arr[0]])" + ] + }, + { + "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": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_quick_sort\n", + "\n", + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_quick_sort\n", + "\n" + ] + } + ], + "source": [ + "from nose.tools import assert_equal\n", + "\n", + "class Test(object):\n", + " def test_quick_sort(self, func):\n", + " print('Empty input')\n", + " data = []\n", + " func(data)\n", + " assert_equal(data, [])\n", + "\n", + " print('One element')\n", + " data = [5]\n", + " func(data)\n", + " assert_equal(data, [5])\n", + "\n", + " print('Two or more elements')\n", + " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", + " data = func(data)\n", + " assert_equal(data, sorted(data))\n", + " \n", + " print('Success: test_quick_sort\\n')\n", + "\n", + "if __name__ == '__main__':\n", + " test = Test()\n", + " test.test_quick_sort(quick_sort)\n", + " test.test_quick_sort(quick_sort_alt)" ] } ],