diff --git a/sorting-searching/merge-sort.ipynb b/sorting-searching/merge-sort.ipynb index 1b406d8..2506a73 100644 --- a/sorting-searching/merge-sort.ipynb +++ b/sorting-searching/merge-sort.ipynb @@ -13,20 +13,22 @@ "source": [ "## Problem: Implement merge 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", - "* Is a naiive merge sort 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?\n", " * Yes" ] }, @@ -73,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -109,25 +111,58 @@ " return merge(left, right)" ] }, + { + "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": 2, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_merge_sort\n" + ] + } + ], "source": [ - "print('Empty input')\n", - "data = []\n", - "merge_sort(data)\n", - "print(data)\n", - "print('One element')\n", - "data = [5]\n", - "merge_sort(data)\n", - "print(data)\n", - "print('Two or more elements')\n", - "data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", - "print(merge_sort(data))" + "from nose.tools import assert_equal\n", + "\n", + "class Test(object):\n", + " def test_merge_sort(self):\n", + " print('Empty input')\n", + " data = []\n", + " merge_sort(data)\n", + " assert_equal(data, [])\n", + "\n", + " print('One element')\n", + " data = [5]\n", + " merge_sort(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 = merge_sort(data)\n", + " assert_equal(data, sorted(data))\n", + " \n", + " print('Success: test_merge_sort')\n", + "\n", + "if __name__ == '__main__':\n", + " test = Test()\n", + " test.test_merge_sort()" ] } ],