mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Reworked notebook: Added more detail to constraints and test cases. Reworked code and unit test.
This commit is contained in:
parent
83e6b5b44d
commit
fea843789b
|
@ -13,10 +13,11 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement quick sort.\n",
|
"## Problem: Implement quick sort.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)\n",
|
"* [Code](#Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)\n",
|
||||||
"* [Pythonic-Code](#Pythonic-Code)"
|
"* [Pythonic-Code](#Pythonic-Code)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -24,9 +25,11 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Constraints and Assumptions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Is a naiive quicksort (ie not in-place) ok?\n",
|
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||||
|
"\n",
|
||||||
|
"* Are you looking for a naiive solution (ie not in-place)?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -73,7 +76,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -98,25 +101,6 @@
|
||||||
" return left + [pivot_value] + right"
|
" return left + [pivot_value] + right"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"print('Empty input')\n",
|
|
||||||
"data = []\n",
|
|
||||||
"print(quick_sort(data))\n",
|
|
||||||
"print('One element')\n",
|
|
||||||
"data = [5]\n",
|
|
||||||
"print(quick_sort(data))\n",
|
|
||||||
"print('Two or more elements')\n",
|
|
||||||
"data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
|
||||||
"print(quick_sort(data))"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -128,17 +112,78 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": true
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def quick_sort(arr): \n",
|
"def quick_sort_alt(arr): \n",
|
||||||
" if len(arr) <= 1:\n",
|
" if len(arr) <= 1:\n",
|
||||||
" return arr\n",
|
" return arr\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" return qsort([x for x in arr[1:] if x<arr[0]]) + [arr[0]] + qsort([x for x in arr[1:] if x>=arr[0]])"
|
" 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": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test\n",
|
||||||
|
"\n",
|
||||||
|
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Empty input\n",
|
||||||
|
"One element\n",
|
||||||
|
"Two or more elements\n",
|
||||||
|
"Success: test_quick_sort\n",
|
||||||
|
"\n",
|
||||||
|
"Empty input\n",
|
||||||
|
"One element\n",
|
||||||
|
"Two or more elements\n",
|
||||||
|
"Success: test_quick_sort\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\n",
|
||||||
|
"class Test(object):\n",
|
||||||
|
" def test_quick_sort(self, func):\n",
|
||||||
|
" print('Empty input')\n",
|
||||||
|
" data = []\n",
|
||||||
|
" func(data)\n",
|
||||||
|
" assert_equal(data, [])\n",
|
||||||
|
"\n",
|
||||||
|
" print('One element')\n",
|
||||||
|
" data = [5]\n",
|
||||||
|
" func(data)\n",
|
||||||
|
" assert_equal(data, [5])\n",
|
||||||
|
"\n",
|
||||||
|
" print('Two or more elements')\n",
|
||||||
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
|
" data = func(data)\n",
|
||||||
|
" assert_equal(data, sorted(data))\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_quick_sort\\n')\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_quick_sort(quick_sort)\n",
|
||||||
|
" test.test_quick_sort(quick_sort_alt)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user