Polish selection sort challenge and solution

This commit is contained in:
Donne Martin 2016-02-24 06:27:54 -05:00
parent 031f3233a9
commit 577ada7896
3 changed files with 86 additions and 41 deletions

View File

@ -117,6 +117,11 @@
"class TestSelectionSort(object):\n",
"\n",
" def test_selection_sort(self, func):\n",
" print('None input')\n",
" data = None\n",
" func(data)\n",
" assert_equal(data, None)\n",
"\n",
" print('Empty input')\n",
" data = []\n",
" func(data)\n",
@ -139,7 +144,8 @@
" test = TestSelectionSort()\n",
" test.test_selection_sort(selection_sort)\n",
" try:\n",
" test.test_selection_sort(selection_sort_iterative)\n",
" test.test_selection_sort(selection_sort_recursive)\n",
" test.test_selection_sort(selection_sort_iterative_alt)\n",
" except NameError:\n",
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
@ -162,21 +168,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,

View File

@ -43,7 +43,8 @@
"source": [
"## Test Cases\n",
"\n",
"* Empty input -> []\n",
"* None -> None\n",
"* [] -> []\n",
"* One element -> [element]\n",
"* Two or more elements"
]
@ -65,7 +66,8 @@
"\n",
"Complexity:\n",
"* Time: O(n^2) average, worst, best\n",
"* Space: O(1) iterative, O(n) recursive (unless tail-call elimination is available, then O(1)), generally not stable"
"* Space: O(1) iterative, O(n) recursive (unless tail-call elimination is available, then O(1)), generally not stable\n",
" * Note: Tail call elimination is not inherently available in Python. See the following [StackOverflow post](http://stackoverflow.com/a/13592002)."
]
},
{
@ -82,6 +84,35 @@
"collapsed": true
},
"outputs": [],
"source": [
"def selection_sort(data):\n",
" if data is None:\n",
" return\n",
" size = len(data)\n",
" for i in range(size):\n",
" if i == size - 1:\n",
" break\n",
" min_index = i\n",
" for j in range(i + 1, size):\n",
" if data[j] < data[min_index]:\n",
" min_index = j\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",
" min_index = start\n",
@ -93,32 +124,20 @@
"\n",
"def swap(data, i, j):\n",
" if (i != j):\n",
" data[i], data[j] = data[j], data[i]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def selection_sort(data, start=0):\n",
" data[i], data[j] = data[j], data[i]\n",
"\n",
"\n",
"def selection_sort_recursive(data, start=0):\n",
" if data is None:\n",
" return None\n",
" if start < len(data) - 1:\n",
" swap(data, start, find_min_index(data, start))\n",
" selection_sort(data, start+1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def selection_sort_iterative(data):\n",
" selection_sort_recursive(data, start+1)\n",
"\n",
"\n",
"def selection_sort_iterative_alt(data):\n",
" if data is None:\n",
" return None\n",
" if len(data) == 0 or len(data) == 1:\n",
" return\n",
" for i in range(0, len(data) - 1):\n",
@ -135,7 +154,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"collapsed": false
},
@ -156,6 +175,11 @@
"class TestSelectionSort(object):\n",
"\n",
" def test_selection_sort(self, func):\n",
" print('None input')\n",
" data = None\n",
" func(data)\n",
" assert_equal(data, None)\n",
"\n",
" print('Empty input')\n",
" data = []\n",
" func(data)\n",
@ -178,7 +202,8 @@
" test = TestSelectionSort()\n",
" test.test_selection_sort(selection_sort)\n",
" try:\n",
" test.test_selection_sort(selection_sort_iterative)\n",
" test.test_selection_sort(selection_sort_recursive)\n",
" test.test_selection_sort(selection_sort_iterative_alt)\n",
" except NameError:\n",
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
@ -191,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"collapsed": false
},
@ -200,11 +225,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
"None input\n",
"Empty input\n",
"One element\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",
"None input\n",
"Empty input\n",
"One element\n",
"Two or more elements\n",
@ -220,21 +253,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,

View File

@ -4,6 +4,11 @@ from nose.tools import assert_equal
class TestSelectionSort(object):
def test_selection_sort(self, func):
print('None input')
data = None
func(data)
assert_equal(data, None)
print('Empty input')
data = []
func(data)
@ -26,7 +31,8 @@ def main():
test = TestSelectionSort()
test.test_selection_sort(selection_sort)
try:
test.test_selection_sort(selection_sort_iterative)
test.test_selection_sort(selection_sort_recursive)
test.test_selection_sort(selection_sort_iterative_alt)
except NameError:
# Alternate solutions are only defined
# in the solutions file