From 3cce1ffb51140982646767c109f7ded2fba94250 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 11 Dec 2016 07:42:54 -0500 Subject: [PATCH] Update insertion sort challenge (#129) Update constraints, test cases, code, and tests. --- .../insertion_sort_challenge.ipynb | 33 +++++++------ .../insertion_sort_solution.ipynb | 49 ++++++++++--------- .../insertion_sort/test_insertion_sort.py | 19 +++---- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb b/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb index 5660130..43ffbd0 100644 --- a/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb +++ b/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb @@ -37,6 +37,10 @@ "* Is a naiive solution sufficient?\n", " * Yes\n", "* Are duplicates allowed?\n", + " * Yes\n", + "* Can we assume the input is valid?\n", + " * No\n", + "* Can we assume this fits memory?\n", " * Yes" ] }, @@ -46,7 +50,7 @@ "source": [ "## Test Cases\n", "\n", - "* None -> None\n", + "* None -> Exception\n", "* Empty input -> []\n", "* One element -> [element]\n", "* Two or more elements" @@ -76,9 +80,11 @@ }, "outputs": [], "source": [ - "def insertion_sort(data):\n", - " # TODO: Implement me\n", - " pass" + "class SelectionSort(object):\n", + "\n", + " def sort(data):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -101,31 +107,26 @@ "outputs": [], "source": [ "# %load test_insertion_sort.py\n", - "from nose.tools import assert_equal\n", + "from nose.tools import assert_equal, assert_raises\n", "\n", "\n", "class TestInsertionSort(object):\n", "\n", " def test_insertion_sort(self):\n", + " insertion_sort = InsertionSort()\n", + "\n", " print('None input')\n", - " data = None\n", - " insertion_sort(data)\n", - " assert_equal(data, None)\n", + " assert_raises(TypeError, insertion_sort.sort, None)\n", "\n", " print('Empty input')\n", - " data = []\n", - " insertion_sort(data)\n", - " assert_equal(data, [])\n", + " assert_equal(insertion_sort.sort([]), [])\n", "\n", " print('One element')\n", - " data = [5]\n", - " insertion_sort(data)\n", - " assert_equal(data, [5])\n", + " assert_equal(insertion_sort.sort([5]), [5])\n", "\n", " print('Two or more elements')\n", " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", - " insertion_sort(data)\n", - " assert_equal(data, sorted(data))\n", + " assert_equal(insertion_sort.sort(data), sorted(data))\n", "\n", " print('Success: test_insertion_sort')\n", "\n", diff --git a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb index 33705ca..16af925 100644 --- a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb +++ b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb @@ -36,6 +36,10 @@ "* Is a naiive solution sufficient?\n", " * Yes\n", "* Are duplicates allowed?\n", + " * Yes\n", + "* Can we assume the input is valid?\n", + " * No\n", + "* Can we assume this fits memory?\n", " * Yes" ] }, @@ -45,7 +49,7 @@ "source": [ "## Test Cases\n", "\n", - "* None -> None\n", + "* None -> Exception\n", "* Empty input -> []\n", "* One element -> [element]\n", "* Two or more elements" @@ -87,16 +91,20 @@ }, "outputs": [], "source": [ - "def insertion_sort(data):\n", - " if data is None or len(data) < 2:\n", - " return\n", - " for r in range(1, len(data)):\n", - " for l in range(r):\n", - " if data[l] > data[r]:\n", - " temp = data[r]\n", - " data[l+1:r+1] = data[l:r]\n", - " data[l] = temp\n", - " break" + "class InsertionSort(object):\n", + "\n", + " def sort(self, data):\n", + " if data is None:\n", + " raise TypeError('data cannot be None')\n", + " if len(data) < 2:\n", + " return data\n", + " for l in range(len(data)-1):\n", + " for r in range(l+1, len(data)):\n", + " if data[r] < data[l]:\n", + " temp = data[r]\n", + " data[l+1:r+1] = data[l:r]\n", + " data[l] = temp\n", + " return data" ] }, { @@ -124,31 +132,26 @@ ], "source": [ "%%writefile test_insertion_sort.py\n", - "from nose.tools import assert_equal\n", + "from nose.tools import assert_equal, assert_raises\n", "\n", "\n", "class TestInsertionSort(object):\n", "\n", " def test_insertion_sort(self):\n", + " insertion_sort = InsertionSort()\n", + "\n", " print('None input')\n", - " data = None\n", - " insertion_sort(data)\n", - " assert_equal(data, None)\n", + " assert_raises(TypeError, insertion_sort.sort, None)\n", "\n", " print('Empty input')\n", - " data = []\n", - " insertion_sort(data)\n", - " assert_equal(data, [])\n", + " assert_equal(insertion_sort.sort([]), [])\n", "\n", " print('One element')\n", - " data = [5]\n", - " insertion_sort(data)\n", - " assert_equal(data, [5])\n", + " assert_equal(insertion_sort.sort([5]), [5])\n", "\n", " print('Two or more elements')\n", " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", - " insertion_sort(data)\n", - " assert_equal(data, sorted(data))\n", + " assert_equal(insertion_sort.sort(data), sorted(data))\n", "\n", " print('Success: test_insertion_sort')\n", "\n", diff --git a/sorting_searching/insertion_sort/test_insertion_sort.py b/sorting_searching/insertion_sort/test_insertion_sort.py index a99ee7e..32046e1 100644 --- a/sorting_searching/insertion_sort/test_insertion_sort.py +++ b/sorting_searching/insertion_sort/test_insertion_sort.py @@ -1,28 +1,23 @@ -from nose.tools import assert_equal +from nose.tools import assert_equal, assert_raises class TestInsertionSort(object): def test_insertion_sort(self): + insertion_sort = InsertionSort() + print('None input') - data = None - insertion_sort(data) - assert_equal(data, None) + assert_raises(TypeError, insertion_sort.sort, None) print('Empty input') - data = [] - insertion_sort(data) - assert_equal(data, []) + assert_equal(insertion_sort.sort([]), []) print('One element') - data = [5] - insertion_sort(data) - assert_equal(data, [5]) + assert_equal(insertion_sort.sort([5]), [5]) print('Two or more elements') data = [5, 1, 7, 2, 6, -3, 5, 7, -1] - insertion_sort(data) - assert_equal(data, sorted(data)) + assert_equal(insertion_sort.sort(data), sorted(data)) print('Success: test_insertion_sort')