mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 10:28:03 +00:00
Polish selection sort challenge and solution
This commit is contained in:
@@ -117,6 +117,11 @@
|
|||||||
"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",
|
||||||
|
" data = None\n",
|
||||||
|
" func(data)\n",
|
||||||
|
" assert_equal(data, None)\n",
|
||||||
|
"\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
" func(data)\n",
|
" func(data)\n",
|
||||||
@@ -139,7 +144,8 @@
|
|||||||
" test = TestSelectionSort()\n",
|
" test = TestSelectionSort()\n",
|
||||||
" test.test_selection_sort(selection_sort)\n",
|
" test.test_selection_sort(selection_sort)\n",
|
||||||
" try:\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",
|
" 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",
|
||||||
@@ -162,21 +168,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.4.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -43,7 +43,8 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Empty input -> []\n",
|
"* None -> None\n",
|
||||||
|
"* [] -> []\n",
|
||||||
"* One element -> [element]\n",
|
"* One element -> [element]\n",
|
||||||
"* Two or more elements"
|
"* Two or more elements"
|
||||||
]
|
]
|
||||||
@@ -65,7 +66,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n^2) average, worst, best\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
|
"collapsed": true
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"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": [
|
"source": [
|
||||||
"def find_min_index(data, start):\n",
|
"def find_min_index(data, start):\n",
|
||||||
" min_index = start\n",
|
" min_index = start\n",
|
||||||
@@ -93,32 +124,20 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"def swap(data, i, j):\n",
|
"def swap(data, i, j):\n",
|
||||||
" if (i != j):\n",
|
" if (i != j):\n",
|
||||||
" data[i], data[j] = data[j], data[i]"
|
" data[i], data[j] = data[j], data[i]\n",
|
||||||
]
|
"\n",
|
||||||
},
|
"\n",
|
||||||
{
|
"def selection_sort_recursive(data, start=0):\n",
|
||||||
"cell_type": "code",
|
" if data is None:\n",
|
||||||
"execution_count": 2,
|
" return None\n",
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"def selection_sort(data, start=0):\n",
|
|
||||||
" if start < len(data) - 1:\n",
|
" if start < len(data) - 1:\n",
|
||||||
" swap(data, start, find_min_index(data, start))\n",
|
" swap(data, start, find_min_index(data, start))\n",
|
||||||
" selection_sort(data, start+1)"
|
" selection_sort_recursive(data, start+1)\n",
|
||||||
]
|
"\n",
|
||||||
},
|
"\n",
|
||||||
{
|
"def selection_sort_iterative_alt(data):\n",
|
||||||
"cell_type": "code",
|
" if data is None:\n",
|
||||||
"execution_count": 3,
|
" return None\n",
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"def selection_sort_iterative(data):\n",
|
|
||||||
" if len(data) == 0 or len(data) == 1:\n",
|
" if len(data) == 0 or len(data) == 1:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" for i in range(0, len(data) - 1):\n",
|
" for i in range(0, len(data) - 1):\n",
|
||||||
@@ -135,7 +154,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -156,6 +175,11 @@
|
|||||||
"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",
|
||||||
|
" data = None\n",
|
||||||
|
" func(data)\n",
|
||||||
|
" assert_equal(data, None)\n",
|
||||||
|
"\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
" func(data)\n",
|
" func(data)\n",
|
||||||
@@ -178,7 +202,8 @@
|
|||||||
" test = TestSelectionSort()\n",
|
" test = TestSelectionSort()\n",
|
||||||
" test.test_selection_sort(selection_sort)\n",
|
" test.test_selection_sort(selection_sort)\n",
|
||||||
" try:\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",
|
" 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",
|
||||||
@@ -191,7 +216,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -200,11 +225,19 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
|
"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",
|
"Success: test_selection_sort\n",
|
||||||
"\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",
|
"Empty input\n",
|
||||||
"One element\n",
|
"One element\n",
|
||||||
"Two or more elements\n",
|
"Two or more elements\n",
|
||||||
@@ -220,21 +253,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.4.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ from nose.tools import assert_equal
|
|||||||
class TestSelectionSort(object):
|
class TestSelectionSort(object):
|
||||||
|
|
||||||
def test_selection_sort(self, func):
|
def test_selection_sort(self, func):
|
||||||
|
print('None input')
|
||||||
|
data = None
|
||||||
|
func(data)
|
||||||
|
assert_equal(data, None)
|
||||||
|
|
||||||
print('Empty input')
|
print('Empty input')
|
||||||
data = []
|
data = []
|
||||||
func(data)
|
func(data)
|
||||||
@@ -26,7 +31,8 @@ def main():
|
|||||||
test = TestSelectionSort()
|
test = TestSelectionSort()
|
||||||
test.test_selection_sort(selection_sort)
|
test.test_selection_sort(selection_sort)
|
||||||
try:
|
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:
|
except NameError:
|
||||||
# Alternate solutions are only defined
|
# Alternate solutions are only defined
|
||||||
# in the solutions file
|
# in the solutions file
|
||||||
|
|||||||
Reference in New Issue
Block a user