diff --git a/sorting-searching/selection-sort.ipynb b/sorting-searching/selection-sort.ipynb index 5bf6ccb..5d6d105 100644 --- a/sorting-searching/selection-sort.ipynb +++ b/sorting-searching/selection-sort.ipynb @@ -13,20 +13,22 @@ "source": [ "## Problem: Implement selection 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", - "* [Pythonic-Code](#Pythonic-Code)" + "* [Unit Test](#Unit-Test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Clarifying Questions\n", + "## Constraints and Assumptions\n", "\n", - "* Are you looking for a generic naiive selection sort (ie not stable)?\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 stable, not based on a heap)?\n", " * Yes" ] }, @@ -70,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -90,34 +92,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "def selection_sort_recursive(data, start):\n", + "def selection_sort_recursive(data, start=0):\n", " if start < len(data) - 1:\n", " swap(data, start, find_min_index(data, start))\n", - " selection_sort_recursive(data, start+1)\n", - "\n", - "print('Empty input')\n", - "data = []\n", - "selection_sort_recursive(data, 0)\n", - "print(data)\n", - "print('One element')\n", - "data = [5]\n", - "selection_sort_recursive(data, 0)\n", - "print(data)\n", - "print('Two or more elements')\n", - "data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", - "selection_sort_recursive(data, 0)\n", - "print(data)" + " selection_sort_recursive(data, start+1)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -129,6 +118,67 @@ " for i in xrange(0, len(data) - 1):\n", " swap(data, i, find_min_index(data, i))" ] + }, + { + "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": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_selection_sort\n", + "\n", + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_selection_sort\n", + "\n" + ] + } + ], + "source": [ + "from nose.tools import assert_equal\n", + "\n", + "class Test(object):\n", + " def test_selection_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", + " func(data)\n", + " assert_equal(data, sorted(data))\n", + " \n", + " print('Success: test_selection_sort\\n')\n", + "\n", + "if __name__ == '__main__':\n", + " test = Test()\n", + " test.test_selection_sort(selection_sort_recursive)\n", + " test.test_selection_sort(selection_sort_iterative)" + ] } ], "metadata": {