mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 02:48:02 +00:00
Added linked list challenge.
This commit is contained in:
@@ -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",
|
||||
"* Is this a singly or doubly linked list?\n",
|
||||
" * Singly\n",
|
||||
@@ -181,8 +188,8 @@
|
||||
],
|
||||
"source": [
|
||||
"%%writefile linked_list.py\n",
|
||||
"\n",
|
||||
"class Node(object):\n",
|
||||
" \n",
|
||||
" def __init__(self, data, next_node=None):\n",
|
||||
" self.next = next_node\n",
|
||||
" self.data = data\n",
|
||||
@@ -191,6 +198,7 @@
|
||||
" return self.data\n",
|
||||
"\n",
|
||||
"class LinkedList(object):\n",
|
||||
" \n",
|
||||
" def __init__(self, head=None):\n",
|
||||
" self.head = head\n",
|
||||
"\n",
|
||||
@@ -287,13 +295,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,
|
||||
@@ -305,39 +306,17 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test: insert_to_front on an empty list\n",
|
||||
"Test: insert_to_front on a NULL\n",
|
||||
"Test: insert_to_front general case\n",
|
||||
"Success: test_insert_to_front\n",
|
||||
"\n",
|
||||
"Test: append on an empty list\n",
|
||||
"Test: append a NULL\n",
|
||||
"Test: append general case\n",
|
||||
"Success: test_append\n",
|
||||
"\n",
|
||||
"Test: find on an empty list\n",
|
||||
"Test: find a NULL\n",
|
||||
"Test: find general case with matches\n",
|
||||
"Test: find general case with no matches\n",
|
||||
"Success: test_find\n",
|
||||
"\n",
|
||||
"Test: delete on an empty list\n",
|
||||
"Test: delete a NULL\n",
|
||||
"Test: delete general case with matches\n",
|
||||
"Test: delete general case with no matches\n",
|
||||
"Success: test_delete\n",
|
||||
"\n",
|
||||
"Test: len on an empty list\n",
|
||||
"Test: len general case\n",
|
||||
"Success: test_len\n",
|
||||
"\n"
|
||||
"Overwriting test_linked_list.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_linked_list.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
"\n",
|
||||
"class TestLinkedList(object):\n",
|
||||
" \n",
|
||||
" def test_insert_to_front(self):\n",
|
||||
" print('Test: insert_to_front on an empty list')\n",
|
||||
" linked_list = LinkedList(None)\n",
|
||||
@@ -438,13 +417,60 @@
|
||||
" \n",
|
||||
" print('Success: test_len\\n')\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
"def main():\n",
|
||||
" test = TestLinkedList()\n",
|
||||
" test.test_insert_to_front()\n",
|
||||
" test.test_append()\n",
|
||||
" test.test_find()\n",
|
||||
" test.test_delete()\n",
|
||||
" test.test_len()"
|
||||
" test.test_len()\n",
|
||||
" \n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test: insert_to_front on an empty list\n",
|
||||
"Test: insert_to_front on a NULL\n",
|
||||
"Test: insert_to_front general case\n",
|
||||
"Success: test_insert_to_front\n",
|
||||
"\n",
|
||||
"Test: append on an empty list\n",
|
||||
"Test: append a NULL\n",
|
||||
"Test: append general case\n",
|
||||
"Success: test_append\n",
|
||||
"\n",
|
||||
"Test: find on an empty list\n",
|
||||
"Test: find a NULL\n",
|
||||
"Test: find general case with matches\n",
|
||||
"Test: find general case with no matches\n",
|
||||
"Success: test_find\n",
|
||||
"\n",
|
||||
"Test: delete on an empty list\n",
|
||||
"Test: delete a NULL\n",
|
||||
"Test: delete general case with matches\n",
|
||||
"Test: delete general case with no matches\n",
|
||||
"Success: test_delete\n",
|
||||
"\n",
|
||||
"Test: len on an empty list\n",
|
||||
"Test: len general case\n",
|
||||
"Success: test_len\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%run -i test_linked_list.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user