Update selection sort challenge (#131)

Update constraints, test cases, code, and tests.
This commit is contained in:
Donne Martin 2016-12-16 07:52:10 -05:00 committed by GitHub
parent bb56b63c39
commit daa7fba255
3 changed files with 90 additions and 126 deletions

View File

@ -37,6 +37,10 @@
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n", "* Is a naiive solution sufficient (ie not stable, not based on a heap)?\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" "* Two or more elements"
@ -76,22 +80,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def selection_sort(data, start=0):\n", "class SelectionSort(object):\n",
" # TODO: Implement me (recursive)\n", "\n",
" pass" " def sort(self, data):\n",
] " # TODO: Implement me (recursive)\n",
}, " pass"
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def selection_sort_iterative(data):\n",
" # TODO: Implement me (iterative)\n",
" pass"
] ]
}, },
{ {
@ -114,41 +107,35 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_selection_sort.py\n", "# %load test_selection_sort.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestSelectionSort(object):\n", "class TestSelectionSort(object):\n",
"\n", "\n",
" def test_selection_sort(self, func):\n", " def test_selection_sort(self, func):\n",
" print('None input')\n", " print('None input')\n",
" data = None\n", " assert_raises(TypeError, func, None)\n",
" func(data)\n",
" assert_equal(data, None)\n",
"\n", "\n",
" print('Empty input')\n", " print('Empty input')\n",
" data = []\n", " assert_equal(func([]), [])\n",
" func(data)\n",
" assert_equal(data, [])\n",
"\n", "\n",
" print('One element')\n", " print('One element')\n",
" data = [5]\n", " assert_equal(func([5]), [5])\n",
" func(data)\n",
" assert_equal(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, -10]\n", " data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n",
" func(data)\n", " assert_equal(func(data), sorted(data))\n",
" assert_equal(data, sorted(data))\n",
"\n", "\n",
" print('Success: test_selection_sort\\n')\n", " print('Success: test_selection_sort\\n')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestSelectionSort()\n", " test = TestSelectionSort()\n",
" test.test_selection_sort(selection_sort)\n", " selection_sort = SelectionSort()\n",
" test.test_selection_sort(selection_sort.sort)\n",
" try:\n", " try:\n",
" test.test_selection_sort(selection_sort_recursive)\n", " test.test_selection_sort(selection_sort.sort_recursive)\n",
" test.test_selection_sort(selection_sort_iterative_alt)\n", " test.test_selection_sort(selection_sort.sor_iterative_alt)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",

View File

@ -36,6 +36,10 @@
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n", "* Is a naiive solution sufficient (ie not stable, not based on a heap)?\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",
"* [] -> []\n", "* [] -> []\n",
"* One element -> [element]\n", "* One element -> [element]\n",
"* Two or more elements" "* Two or more elements"
@ -87,64 +91,57 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def selection_sort(data):\n", "class SelectionSort(object):\n",
" if data is None or len(data) < 2:\n", "\n",
" return\n", " def sort(self, data):\n",
" for i in range(len(data)-1):\n", " if data is None:\n",
" min_index = i\n", " raise TypeError('data cannot be None')\n",
" for j in range(i + 1, len(data)):\n", " if len(data) < 2:\n",
" if data[j] < data[min_index]:\n", " return data\n",
" min_index = j\n", " for i in range(len(data)-1):\n",
" data[i], data[min_index] = data[min_index], data[i]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below are alternate solutions with helper functions:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def find_min_index(data, start):\n",
" # Raises an exception if: \n",
" # -data is None\n",
" # -start >= len(data) - 1\n",
" min_index = start\n",
" for i in range(start + 1, len(data)):\n",
" if data[i] < data[min_index]:\n",
" min_index = i\n", " min_index = i\n",
" return min_index\n", " for j in range(i+1, len(data)):\n",
" if data[j] < data[min_index]:\n",
" min_index = j\n",
" if data[min_index] < data[i]:\n",
" data[i], data[min_index] = data[min_index], data[i]\n",
" return data\n",
"\n", "\n",
" def sort_iterative_alt(self, data):\n",
" if data is None:\n",
" raise TypeError('data cannot be None')\n",
" if len(data) < 2:\n",
" return data\n",
" for i in range(len(data)-1):\n",
" self._swap(data, i, self._find_min_index(data, i))\n",
" return data\n",
"\n", "\n",
"def swap(data, i, j):\n", " def sort_recursive(self, data, start=0):\n",
" # Raises an exception if: \n", " if data is None:\n",
" # -data is None\n", " raise TypeError('data cannot be None')\n",
" # -i or j are >= len(data)\n", " if len(data) < 2:\n",
" if i != j:\n", " return data\n",
" data[i], data[j] = data[j], data[i]\n", " return self._sort_recursive(data, start)\n",
"\n", "\n",
" def _sort_recursive(self, data, start):\n",
" if data is None:\n",
" return\n",
" if start < len(data) - 1:\n",
" swap(data, start, self._find_min_index(data, start))\n",
" self._sort_recursive(data, start+1)\n",
" return data\n",
"\n", "\n",
"def selection_sort_recursive(data, start=0):\n", " def _find_min_index(self, data, start):\n",
" if data is None:\n", " min_index = start\n",
" return\n", " for i in range(start+1, len(data)):\n",
" if start < len(data) - 1:\n", " if data[i] < data[min_index]:\n",
" swap(data, start, find_min_index(data, start))\n", " min_index = i\n",
" selection_sort_recursive(data, start+1)\n", " return min_index\n",
"\n", "\n",
"\n", " def _swap(self, data, i, j):\n",
"def selection_sort_iterative_alt(data):\n", " if i != j:\n",
" if data is None or len(data) < 2:\n", " data[i], data[j] = data[j], data[i]\n",
" return\n", " return data"
" for i in range(len(data)-1):\n",
" swap(data, i, find_min_index(data, i))"
] ]
}, },
{ {
@ -157,7 +154,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -172,41 +169,35 @@
], ],
"source": [ "source": [
"%%writefile test_selection_sort.py\n", "%%writefile test_selection_sort.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestSelectionSort(object):\n", "class TestSelectionSort(object):\n",
"\n", "\n",
" def test_selection_sort(self, func):\n", " def test_selection_sort(self, func):\n",
" print('None input')\n", " print('None input')\n",
" data = None\n", " assert_raises(TypeError, func, None)\n",
" func(data)\n",
" assert_equal(data, None)\n",
"\n", "\n",
" print('Empty input')\n", " print('Empty input')\n",
" data = []\n", " assert_equal(func([]), [])\n",
" func(data)\n",
" assert_equal(data, [])\n",
"\n", "\n",
" print('One element')\n", " print('One element')\n",
" data = [5]\n", " assert_equal(func([5]), [5])\n",
" func(data)\n",
" assert_equal(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, -10]\n", " data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n",
" func(data)\n", " assert_equal(func(data), sorted(data))\n",
" assert_equal(data, sorted(data))\n",
"\n", "\n",
" print('Success: test_selection_sort\\n')\n", " print('Success: test_selection_sort\\n')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestSelectionSort()\n", " test = TestSelectionSort()\n",
" test.test_selection_sort(selection_sort)\n", " selection_sort = SelectionSort()\n",
" test.test_selection_sort(selection_sort.sort)\n",
" try:\n", " try:\n",
" test.test_selection_sort(selection_sort_recursive)\n", " test.test_selection_sort(selection_sort.sort_recursive)\n",
" test.test_selection_sort(selection_sort_iterative_alt)\n", " test.test_selection_sort(selection_sort.sor_iterative_alt)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",
@ -219,7 +210,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -237,15 +228,7 @@
"None input\n", "None input\n",
"Empty input\n", "Empty input\n",
"One element\n", "One element\n",
"Two or more elements\n", "Two or more elements\n"
"Success: test_selection_sort\n",
"\n",
"None input\n",
"Empty input\n",
"One element\n",
"Two or more elements\n",
"Success: test_selection_sort\n",
"\n"
] ]
} }
], ],

View File

@ -1,38 +1,32 @@
from nose.tools import assert_equal from nose.tools import assert_equal, assert_raises
class TestSelectionSort(object): class TestSelectionSort(object):
def test_selection_sort(self, func): def test_selection_sort(self, func):
print('None input') print('None input')
data = None assert_raises(TypeError, func, None)
func(data)
assert_equal(data, None)
print('Empty input') print('Empty input')
data = [] assert_equal(func([]), [])
func(data)
assert_equal(data, [])
print('One element') print('One element')
data = [5] assert_equal(func([5]), [5])
func(data)
assert_equal(data, [5])
print('Two or more elements') print('Two or more elements')
data = [5, 1, 7, 2, 6, -3, 5, 7, -10] data = [5, 1, 7, 2, 6, -3, 5, 7, -10]
func(data) assert_equal(func(data), sorted(data))
assert_equal(data, sorted(data))
print('Success: test_selection_sort\n') print('Success: test_selection_sort\n')
def main(): def main():
test = TestSelectionSort() test = TestSelectionSort()
test.test_selection_sort(selection_sort) selection_sort = SelectionSort()
test.test_selection_sort(selection_sort.sort)
try: try:
test.test_selection_sort(selection_sort_recursive) test.test_selection_sort(selection_sort.sort_recursive)
test.test_selection_sort(selection_sort_iterative_alt) test.test_selection_sort(selection_sort.sor_iterative_alt)
except NameError: except NameError:
# Alternate solutions are only defined # Alternate solutions are only defined
# in the solutions file # in the solutions file