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 unit test.
This commit is contained in:
parent
0c43a34ae1
commit
9c36d2dc0a
|
@ -13,20 +13,23 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement insertion sort.\n",
|
"## Problem: Implement insertion 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",
|
||||||
"* [Pythonic-Code](#Pythonic-Code)"
|
"* [Unit Test](#Unit-Test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Constraints and Assumptions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None"
|
"*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?\n",
|
||||||
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -70,7 +73,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -89,25 +92,57 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "markdown",
|
||||||
"execution_count": null,
|
"metadata": {},
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print('Empty input')\n",
|
"## Unit Test\n",
|
||||||
"data = []\n",
|
"\n",
|
||||||
"insertion_sort(data)\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.*"
|
||||||
"print(data)\n",
|
]
|
||||||
"print('One element')\n",
|
},
|
||||||
"data = [5]\n",
|
{
|
||||||
"insertion_sort(data)\n",
|
"cell_type": "code",
|
||||||
"print(data)\n",
|
"execution_count": 2,
|
||||||
"print('Two or more elements')\n",
|
"metadata": {
|
||||||
"data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
"collapsed": false
|
||||||
"insertion_sort(data)\n",
|
},
|
||||||
"print(data)"
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Empty input\n",
|
||||||
|
"One element\n",
|
||||||
|
"Two or more elements\n",
|
||||||
|
"Success: test_insertion_sort\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\n",
|
||||||
|
"class Test(object):\n",
|
||||||
|
" def test_insertion_sort(self):\n",
|
||||||
|
" print('Empty input')\n",
|
||||||
|
" data = []\n",
|
||||||
|
" insertion_sort(data)\n",
|
||||||
|
" assert_equal(data, [])\n",
|
||||||
|
"\n",
|
||||||
|
" print('One element')\n",
|
||||||
|
" data = [5]\n",
|
||||||
|
" insertion_sort(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",
|
||||||
|
" insertion_sort(data)\n",
|
||||||
|
" assert_equal(data, sorted(data))\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_insertion_sort')\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_insertion_sort()"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user