mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08: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:
parent
4b5ce09e6c
commit
15ed55a7d4
@ -44,6 +44,7 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* Empty input -> []\n",
|
||||
"* One element -> [element]\n",
|
||||
"* Two or more elements"
|
||||
@ -103,6 +104,11 @@
|
||||
"\n",
|
||||
"class TestQuickSort(object):\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",
|
||||
" data = []\n",
|
||||
" sorted_data = func(data)\n",
|
||||
@ -124,12 +130,6 @@
|
||||
"def main():\n",
|
||||
" test = TestQuickSort()\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",
|
||||
"if __name__ == '__main__':\n",
|
||||
@ -148,21 +148,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,
|
||||
|
@ -44,6 +44,7 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* Empty input -> []\n",
|
||||
"* One element -> [element]\n",
|
||||
"* Two or more elements"
|
||||
@ -69,7 +70,9 @@
|
||||
"\n",
|
||||
"Complexity:\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",
|
||||
"def quick_sort(data):\n",
|
||||
" if len(data) < 2:\n",
|
||||
" if data is None or len(data) < 2:\n",
|
||||
" return data\n",
|
||||
" left = []\n",
|
||||
" right = []\n",
|
||||
@ -113,30 +116,6 @@
|
||||
" 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",
|
||||
"metadata": {},
|
||||
@ -147,7 +126,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@ -167,6 +146,11 @@
|
||||
"\n",
|
||||
"class TestQuickSort(object):\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",
|
||||
" data = []\n",
|
||||
" sorted_data = func(data)\n",
|
||||
@ -188,12 +172,6 @@
|
||||
"def main():\n",
|
||||
" test = TestQuickSort()\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",
|
||||
"if __name__ == '__main__':\n",
|
||||
@ -202,7 +180,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@ -211,11 +189,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Empty input\n",
|
||||
"One element\n",
|
||||
"Two or more elements\n",
|
||||
"Success: test_quick_sort\n",
|
||||
"\n",
|
||||
"None input\n",
|
||||
"Empty input\n",
|
||||
"One element\n",
|
||||
"Two or more elements\n",
|
||||
@ -231,21 +205,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,
|
||||
|
@ -3,6 +3,11 @@ from nose.tools import assert_equal
|
||||
|
||||
class TestQuickSort(object):
|
||||
def test_quick_sort(self, func):
|
||||
print('None input')
|
||||
data = None
|
||||
sorted_data = func(data)
|
||||
assert_equal(sorted_data, None)
|
||||
|
||||
print('Empty input')
|
||||
data = []
|
||||
sorted_data = func(data)
|
||||
@ -24,12 +29,6 @@ class TestQuickSort(object):
|
||||
def main():
|
||||
test = TestQuickSort()
|
||||
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__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user