Update merge sort challenge (#130)

Update constraints, test cases, code, and tests.
This commit is contained in:
Donne Martin 2016-12-12 07:22:16 -05:00 committed by GitHub
parent 3cce1ffb51
commit bb56b63c39
3 changed files with 71 additions and 68 deletions

View File

@ -37,6 +37,10 @@
"* Is a naiive solution sufficient?\n", "* Is a naiive solution sufficient?\n",
" * Yes\n", " * Yes\n",
"* Are duplicates allowed?\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" " * Yes"
] ]
}, },
@ -46,7 +50,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* None -> None\n", "* None -> Exception\n",
"* Empty input -> []\n", "* Empty input -> []\n",
"* One element -> [element]\n", "* One element -> [element]\n",
"* Two or more elements\n", "* Two or more elements\n",
@ -77,7 +81,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def merge_sort(data):\n", "class MergeSort(object):\n",
"\n",
" def sort(self, data):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
] ]
@ -102,30 +108,26 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_merge_sort.py\n", "# %load test_merge_sort.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestMergeSort(object):\n", "class TestMergeSort(object):\n",
"\n",
" def test_merge_sort(self):\n", " def test_merge_sort(self):\n",
" merge_sort = MergeSort()\n",
"\n",
" print('None input')\n", " print('None input')\n",
" data = None\n", " assert_raises(TypeError, merge_sort.sort, None)\n",
" sorted_data = merge_sort(data)\n",
" assert_equal(sorted_data, None)\n",
"\n", "\n",
" print('Empty input')\n", " print('Empty input')\n",
" data = []\n", " assert_equal(merge_sort.sort([]), [])\n",
" sorted_data = merge_sort(data)\n",
" assert_equal(sorted_data, [])\n",
"\n", "\n",
" print('One element')\n", " print('One element')\n",
" data = [5]\n", " assert_equal(merge_sort.sort([5]), [5])\n",
" sorted_data = merge_sort(data)\n",
" assert_equal(sorted_data, [5])\n",
"\n", "\n",
" print('Two or more elements')\n", " print('Two or more elements')\n",
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
" sorted_data = merge_sort(data)\n", " assert_equal(merge_sort.sort(data), sorted(data))\n",
" assert_equal(sorted_data, sorted(data))\n",
"\n", "\n",
" print('Success: test_merge_sort')\n", " print('Success: test_merge_sort')\n",
"\n", "\n",

View File

@ -36,6 +36,10 @@
"* Is a naiive solution sufficient?\n", "* Is a naiive solution sufficient?\n",
" * Yes\n", " * Yes\n",
"* Are duplicates allowed?\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" " * Yes"
] ]
}, },
@ -45,7 +49,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* None -> None\n", "* None -> Exception\n",
"* Empty input -> []\n", "* Empty input -> []\n",
"* One element -> [element]\n", "* One element -> [element]\n",
"* Two or more elements\n", "* Two or more elements\n",
@ -94,7 +98,24 @@
"from __future__ import division\n", "from __future__ import division\n",
"\n", "\n",
"\n", "\n",
"def merge(left, right):\n", "class MergeSort(object):\n",
"\n",
" def sort(self, data):\n",
" if data is None:\n",
" raise TypeError('data cannot be None')\n",
" return self._sort(data)\n",
"\n",
" def _sort(self, data):\n",
" if len(data) < 2:\n",
" return data\n",
" mid = len(data) // 2\n",
" left = data[:mid]\n",
" right = data[mid:]\n",
" left = self._sort(left)\n",
" right = self._sort(right)\n",
" return self._merge(left, right)\n",
"\n",
" def _merge(self, left, right):\n",
" l = 0\n", " l = 0\n",
" r = 0\n", " r = 0\n",
" result = []\n", " result = []\n",
@ -105,7 +126,6 @@
" else:\n", " else:\n",
" result.append(right[r])\n", " result.append(right[r])\n",
" r += 1\n", " r += 1\n",
" \n",
" # Copy remaining elements\n", " # Copy remaining elements\n",
" while l < len(left):\n", " while l < len(left):\n",
" result.append(left[l])\n", " result.append(left[l])\n",
@ -113,18 +133,7 @@
" while r < len(right):\n", " while r < len(right):\n",
" result.append(right[r])\n", " result.append(right[r])\n",
" r += 1\n", " r += 1\n",
" return result\n", " return result"
"\n",
"\n",
"def merge_sort(data):\n",
" if data is None or len(data) < 2:\n",
" return data\n",
" mid = len(data) // 2\n",
" left = data[:mid]\n",
" right = data[mid:]\n",
" left = merge_sort(left)\n",
" right = merge_sort(right)\n",
" return merge(left, right)"
] ]
}, },
{ {
@ -152,30 +161,26 @@
], ],
"source": [ "source": [
"%%writefile test_merge_sort.py\n", "%%writefile test_merge_sort.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestMergeSort(object):\n", "class TestMergeSort(object):\n",
"\n",
" def test_merge_sort(self):\n", " def test_merge_sort(self):\n",
" merge_sort = MergeSort()\n",
"\n",
" print('None input')\n", " print('None input')\n",
" data = None\n", " assert_raises(TypeError, merge_sort.sort, None)\n",
" sorted_data = merge_sort(data)\n",
" assert_equal(sorted_data, None)\n",
"\n", "\n",
" print('Empty input')\n", " print('Empty input')\n",
" data = []\n", " assert_equal(merge_sort.sort([]), [])\n",
" sorted_data = merge_sort(data)\n",
" assert_equal(sorted_data, [])\n",
"\n", "\n",
" print('One element')\n", " print('One element')\n",
" data = [5]\n", " assert_equal(merge_sort.sort([5]), [5])\n",
" sorted_data = merge_sort(data)\n",
" assert_equal(sorted_data, [5])\n",
"\n", "\n",
" print('Two or more elements')\n", " print('Two or more elements')\n",
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
" sorted_data = merge_sort(data)\n", " assert_equal(merge_sort.sort(data), sorted(data))\n",
" assert_equal(sorted_data, sorted(data))\n",
"\n", "\n",
" print('Success: test_merge_sort')\n", " print('Success: test_merge_sort')\n",
"\n", "\n",

View File

@ -1,27 +1,23 @@
from nose.tools import assert_equal from nose.tools import assert_equal, assert_raises
class TestMergeSort(object): class TestMergeSort(object):
def test_merge_sort(self): def test_merge_sort(self):
merge_sort = MergeSort()
print('None input') print('None input')
data = None assert_raises(TypeError, merge_sort.sort, None)
sorted_data = merge_sort(data)
assert_equal(sorted_data, None)
print('Empty input') print('Empty input')
data = [] assert_equal(merge_sort.sort([]), [])
sorted_data = merge_sort(data)
assert_equal(sorted_data, [])
print('One element') print('One element')
data = [5] assert_equal(merge_sort.sort([5]), [5])
sorted_data = merge_sort(data)
assert_equal(sorted_data, [5])
print('Two or more elements') print('Two or more elements')
data = [5, 1, 7, 2, 6, -3, 5, 7, -1] data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
sorted_data = merge_sort(data) assert_equal(merge_sort.sort(data), sorted(data))
assert_equal(sorted_data, sorted(data))
print('Success: test_merge_sort') print('Success: test_merge_sort')