Added partition challenge.

This commit is contained in:
Donne Martin
2015-07-02 23:11:35 -04:00
parent d79ee29ec8
commit c16f25e5f3
4 changed files with 302 additions and 20 deletions

View File

@@ -4,7 +4,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
@@ -26,7 +33,7 @@
"source": [
"## Constraints\n",
"\n",
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n",
"* Can we create additional data structures?\n",
" * Yes\n",
@@ -44,8 +51,8 @@
"source": [
"## Test Cases\n",
"\n",
"* Empty list\n",
"* One element list\n",
"* Empty list -> []\n",
"* One element list -> [element]\n",
"* Left linked list is empty\n",
"* Right linked list is empty\n",
"* General case\n",
@@ -86,7 +93,7 @@
},
"outputs": [],
"source": [
"%run linked_list.py"
"%run ../linked_list/linked_list.py"
]
},
{
@@ -98,6 +105,7 @@
"outputs": [],
"source": [
"class MyLinkedList(LinkedList):\n",
" \n",
" def partition(self, data):\n",
" if self.head is None:\n",
" return\n",
@@ -130,13 +138,6 @@
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*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,
@@ -148,18 +149,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Empty list\n",
"Test: One element list, left list empty\n",
"Test: Right list is empty\n",
"Test: General case\n",
"Success: test_partition\n"
"Overwriting test_partition.py\n"
]
}
],
"source": [
"%%writefile test_partition.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
"\n",
"class TestPartition(object):\n",
" \n",
" def test_partition(self):\n",
" print('Test: Empty list')\n",
" linked_list = MyLinkedList(None)\n",
@@ -194,9 +194,35 @@
" \n",
" print('Success: test_partition')\n",
"\n",
"def main():\n",
" test = TestPartition()\n",
" test.test_partition()\n",
" \n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_partition()"
" main()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Empty list\n",
"Test: One element list, left list empty\n",
"Test: Right list is empty\n",
"Test: General case\n",
"Success: test_partition\n"
]
}
],
"source": [
"run -i test_partition.py"
]
}
],