mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish merge sort challenge and solution
Add None input test case. Update space complexity discussion.
This commit is contained in:
parent
15ed55a7d4
commit
56f8ea1e74
|
@ -44,6 +44,7 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* Empty input -> []\n",
|
||||
"* One element -> [element]\n",
|
||||
"* Two or more elements\n",
|
||||
|
@ -104,6 +105,11 @@
|
|||
"\n",
|
||||
"class TestMergeSort(object):\n",
|
||||
" def test_merge_sort(self):\n",
|
||||
" print('None input')\n",
|
||||
" data = None\n",
|
||||
" sorted_data = merge_sort(data)\n",
|
||||
" assert_equal(sorted_data, None)\n",
|
||||
"\n",
|
||||
" print('Empty input')\n",
|
||||
" data = []\n",
|
||||
" sorted_data = merge_sort(data)\n",
|
||||
|
@ -143,21 +149,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,
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* Empty input -> []\n",
|
||||
"* One element -> [element]\n",
|
||||
"* Two or more elements\n",
|
||||
|
@ -68,7 +69,9 @@
|
|||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n log(n))\n",
|
||||
"* Space: O(n), stable"
|
||||
"* Space: O(n+m), n = number of elements, m = recursion depth\n",
|
||||
"\n",
|
||||
"Most implementations are stable."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -112,11 +115,11 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"def merge_sort(data):\n",
|
||||
" if len(data) < 2:\n",
|
||||
" if data is None or len(data) < 2:\n",
|
||||
" return data\n",
|
||||
" mid = len(data) // 2\n",
|
||||
" left = data[0:mid]\n",
|
||||
" right = data[mid:len(data)]\n",
|
||||
" right = data[mid:]\n",
|
||||
" left = merge_sort(left)\n",
|
||||
" right = merge_sort(right)\n",
|
||||
" return merge(left, right)"
|
||||
|
@ -152,6 +155,11 @@
|
|||
"\n",
|
||||
"class TestMergeSort(object):\n",
|
||||
" def test_merge_sort(self):\n",
|
||||
" print('None input')\n",
|
||||
" data = None\n",
|
||||
" sorted_data = merge_sort(data)\n",
|
||||
" assert_equal(sorted_data, None)\n",
|
||||
"\n",
|
||||
" print('Empty input')\n",
|
||||
" data = []\n",
|
||||
" sorted_data = merge_sort(data)\n",
|
||||
|
@ -190,6 +198,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"None input\n",
|
||||
"Empty input\n",
|
||||
"One element\n",
|
||||
"Two or more elements\n",
|
||||
|
@ -204,21 +213,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 TestMergeSort(object):
|
||||
def test_merge_sort(self):
|
||||
print('None input')
|
||||
data = None
|
||||
sorted_data = merge_sort(data)
|
||||
assert_equal(sorted_data, None)
|
||||
|
||||
print('Empty input')
|
||||
data = []
|
||||
sorted_data = merge_sort(data)
|
||||
|
|
Loading…
Reference in New Issue
Block a user