mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 01:48:02 +00:00
Polish quicksort challenge and solution
Add None input test case. Remove difficult to read 'Pythonic' solution. Update space complexity discussion.
This commit is contained in:
@@ -44,6 +44,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"* None -> None\n",
|
||||||
"* Empty input -> []\n",
|
"* Empty input -> []\n",
|
||||||
"* One element -> [element]\n",
|
"* One element -> [element]\n",
|
||||||
"* Two or more elements"
|
"* Two or more elements"
|
||||||
@@ -103,6 +104,11 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"class TestQuickSort(object):\n",
|
"class TestQuickSort(object):\n",
|
||||||
" def test_quick_sort(self, func):\n",
|
" def test_quick_sort(self, func):\n",
|
||||||
|
" print('None input')\n",
|
||||||
|
" data = None\n",
|
||||||
|
" sorted_data = func(data)\n",
|
||||||
|
" assert_equal(sorted_data, None)\n",
|
||||||
|
"\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
" sorted_data = func(data)\n",
|
" sorted_data = func(data)\n",
|
||||||
@@ -124,12 +130,6 @@
|
|||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQuickSort()\n",
|
" test = TestQuickSort()\n",
|
||||||
" test.test_quick_sort(quick_sort)\n",
|
" test.test_quick_sort(quick_sort)\n",
|
||||||
" try:\n",
|
|
||||||
" test.test_quick_sort(quick_sort_alt)\n",
|
|
||||||
" except NameError:\n",
|
|
||||||
" # Alternate solutions are only defined\n",
|
|
||||||
" # in the solutions file\n",
|
|
||||||
" pass\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
@@ -148,21 +148,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,
|
||||||
|
|||||||
@@ -44,6 +44,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"* None -> None\n",
|
||||||
"* Empty input -> []\n",
|
"* Empty input -> []\n",
|
||||||
"* One element -> [element]\n",
|
"* One element -> [element]\n",
|
||||||
"* Two or more elements"
|
"* Two or more elements"
|
||||||
@@ -69,7 +70,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n log(n)) average, best, O(n^2) worst\n",
|
"* Time: O(n log(n)) average, best, O(n^2) worst\n",
|
||||||
"* Space: O(n), n extra space, n recursion depth, generally not stable"
|
"* Space: O(n+m), n = number of elements, m = recursion depth\n",
|
||||||
|
"\n",
|
||||||
|
"Most implementations are not stable."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -91,7 +94,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def quick_sort(data):\n",
|
"def quick_sort(data):\n",
|
||||||
" if len(data) < 2:\n",
|
" if data is None or len(data) < 2:\n",
|
||||||
" return data\n",
|
" return data\n",
|
||||||
" left = []\n",
|
" left = []\n",
|
||||||
" right = []\n",
|
" right = []\n",
|
||||||
@@ -113,30 +116,6 @@
|
|||||||
" return left + [pivot_value] + right"
|
" return left + [pivot_value] + right"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Pythonic-Code\n",
|
|
||||||
"\n",
|
|
||||||
"The following code from [Stack Overflow](http://stackoverflow.com/questions/18262306/quick-sort-with-python) is very concise, although it might be a little difficult to read:"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 2,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"def quick_sort_alt(arr):\n",
|
|
||||||
" if len(arr) <= 1:\n",
|
|
||||||
" return arr\n",
|
|
||||||
" else:\n",
|
|
||||||
" return quick_sort_alt([x for x in arr[1:] if x < arr[0]]) + [arr[0]] + quick_sort_alt([x for x in arr[1:] if x >= arr[0]])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@@ -147,7 +126,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -167,6 +146,11 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"class TestQuickSort(object):\n",
|
"class TestQuickSort(object):\n",
|
||||||
" def test_quick_sort(self, func):\n",
|
" def test_quick_sort(self, func):\n",
|
||||||
|
" print('None input')\n",
|
||||||
|
" data = None\n",
|
||||||
|
" sorted_data = func(data)\n",
|
||||||
|
" assert_equal(sorted_data, None)\n",
|
||||||
|
"\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
" sorted_data = func(data)\n",
|
" sorted_data = func(data)\n",
|
||||||
@@ -188,12 +172,6 @@
|
|||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQuickSort()\n",
|
" test = TestQuickSort()\n",
|
||||||
" test.test_quick_sort(quick_sort)\n",
|
" test.test_quick_sort(quick_sort)\n",
|
||||||
" try:\n",
|
|
||||||
" test.test_quick_sort(quick_sort_alt)\n",
|
|
||||||
" except NameError:\n",
|
|
||||||
" # Alternate solutions are only defined\n",
|
|
||||||
" # in the solutions file\n",
|
|
||||||
" pass\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
@@ -202,7 +180,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -211,11 +189,7 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Empty input\n",
|
"None input\n",
|
||||||
"One element\n",
|
|
||||||
"Two or more elements\n",
|
|
||||||
"Success: test_quick_sort\n",
|
|
||||||
"\n",
|
|
||||||
"Empty input\n",
|
"Empty input\n",
|
||||||
"One element\n",
|
"One element\n",
|
||||||
"Two or more elements\n",
|
"Two or more elements\n",
|
||||||
@@ -231,21 +205,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,
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ from nose.tools import assert_equal
|
|||||||
|
|
||||||
class TestQuickSort(object):
|
class TestQuickSort(object):
|
||||||
def test_quick_sort(self, func):
|
def test_quick_sort(self, func):
|
||||||
|
print('None input')
|
||||||
|
data = None
|
||||||
|
sorted_data = func(data)
|
||||||
|
assert_equal(sorted_data, None)
|
||||||
|
|
||||||
print('Empty input')
|
print('Empty input')
|
||||||
data = []
|
data = []
|
||||||
sorted_data = func(data)
|
sorted_data = func(data)
|
||||||
@@ -24,12 +29,6 @@ class TestQuickSort(object):
|
|||||||
def main():
|
def main():
|
||||||
test = TestQuickSort()
|
test = TestQuickSort()
|
||||||
test.test_quick_sort(quick_sort)
|
test.test_quick_sort(quick_sort)
|
||||||
try:
|
|
||||||
test.test_quick_sort(quick_sort_alt)
|
|
||||||
except NameError:
|
|
||||||
# Alternate solutions are only defined
|
|
||||||
# in the solutions file
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user