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 algorithm discussion, code, and unit test.
This commit is contained in:
parent
9490979fe5
commit
585f5b8978
|
@ -13,17 +13,20 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement a linked list with insert, append, find, delete, length, and print methods\n",
|
"## Problem: Implement a linked list with insert, append, find, delete, length, and print methods\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)"
|
"* [Code](#Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Constraints and Assumptions\n",
|
||||||
|
"\n",
|
||||||
|
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Is this a singly or doubly linked list?\n",
|
"* Is this a singly or doubly linked list?\n",
|
||||||
" * Singly\n",
|
" * Singly\n",
|
||||||
|
@ -163,11 +166,19 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Overwriting linked_list.py\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile linked_list.py\n",
|
"%%writefile linked_list.py\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -200,6 +211,7 @@
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" node.next = self.head\n",
|
" node.next = self.head\n",
|
||||||
" self.head = node\n",
|
" self.head = node\n",
|
||||||
|
" return node\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def append(self, data, next_node=None):\n",
|
" def append(self, data, next_node=None):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
|
@ -212,6 +224,7 @@
|
||||||
" while curr_node.next is not None:\n",
|
" while curr_node.next is not None:\n",
|
||||||
" curr_node = curr_node.next\n",
|
" curr_node = curr_node.next\n",
|
||||||
" curr_node.next = node\n",
|
" curr_node.next = node\n",
|
||||||
|
" return node\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def find(self, data):\n",
|
" def find(self, data):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
|
@ -224,6 +237,7 @@
|
||||||
" return curr_node\n",
|
" return curr_node\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" curr_node = curr_node.next\n",
|
" curr_node = curr_node.next\n",
|
||||||
|
" return\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def delete(self, data):\n",
|
" def delete(self, data):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
|
@ -244,14 +258,22 @@
|
||||||
" curr_node = self.head\n",
|
" curr_node = self.head\n",
|
||||||
" while curr_node is not None:\n",
|
" while curr_node is not None:\n",
|
||||||
" print(curr_node.data)\n",
|
" print(curr_node.data)\n",
|
||||||
" curr_node = curr_node.next"
|
" curr_node = curr_node.next\n",
|
||||||
|
"\n",
|
||||||
|
" def get_all_data(self):\n",
|
||||||
|
" data = []\n",
|
||||||
|
" curr_node = self.head\n",
|
||||||
|
" while curr_node is not None:\n",
|
||||||
|
" data.append(curr_node.data)\n",
|
||||||
|
" curr_node = curr_node.next\n",
|
||||||
|
" return data"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -259,139 +281,170 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "markdown",
|
||||||
"execution_count": null,
|
"metadata": {},
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Test insert_to_front\n",
|
"## Unit Test"
|
||||||
"# Insert in an empty list\n",
|
]
|
||||||
"linked_list = LinkedList(None)\n",
|
},
|
||||||
"linked_list.insert_to_front(10)\n",
|
{
|
||||||
"linked_list.print_list()\n",
|
"cell_type": "markdown",
|
||||||
"# Insert a NULL\n",
|
"metadata": {},
|
||||||
"linked_list.insert_to_front(None)\n",
|
"source": [
|
||||||
"linked_list.print_list()\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.*"
|
||||||
"# Insert in a list with one element or more elements\n",
|
|
||||||
"linked_list.insert_to_front('a')\n",
|
|
||||||
"linked_list.insert_to_front('bc')\n",
|
|
||||||
"linked_list.print_list()"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
"source": [
|
|
||||||
"# Test append\n",
|
|
||||||
"# Insert in an empty list\n",
|
|
||||||
"linked_list = LinkedList(None)\n",
|
|
||||||
"linked_list.append(10)\n",
|
|
||||||
"linked_list.print_list()\n",
|
|
||||||
"# Insert a NULL\n",
|
|
||||||
"linked_list.append(None)\n",
|
|
||||||
"linked_list.print_list()\n",
|
|
||||||
"# Insert in a list with one element or more elements\n",
|
|
||||||
"linked_list.append('a')\n",
|
|
||||||
"linked_list.append('bc')\n",
|
|
||||||
"linked_list.print_list()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"name": "stdout",
|
||||||
"execution_count": null,
|
"output_type": "stream",
|
||||||
"metadata": {
|
"text": [
|
||||||
"collapsed": false
|
"Test: insert_to_front on an empty list\n",
|
||||||
},
|
"Test: insert_to_front on a NULL\n",
|
||||||
"outputs": [],
|
"Test: insert_to_front general case\n",
|
||||||
"source": [
|
"Success: test_insert_to_front\n",
|
||||||
"# Test find\n",
|
"\n",
|
||||||
"# Find in an empty list\n",
|
"Test: append on an empty list\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"Test: append a NULL\n",
|
||||||
"node = linked_list.find('a')\n",
|
"Test: append general case\n",
|
||||||
"print(node)\n",
|
"Success: test_append\n",
|
||||||
"# Find a NULL\n",
|
"\n",
|
||||||
"head = Node(10)\n",
|
"Test: find on an empty list\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"Test: find a NULL\n",
|
||||||
"node = linked_list.find(None)\n",
|
"Test: find general case with matches\n",
|
||||||
"print(node)\n",
|
"Test: find general case with no matches\n",
|
||||||
"# Find in a list with one element or more matching elements\n",
|
"Success: test_find\n",
|
||||||
"head = Node(10)\n",
|
"\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"Test: delete on an empty list\n",
|
||||||
"linked_list.insert_to_front('a')\n",
|
"Test: delete a NULL\n",
|
||||||
"linked_list.insert_to_front('bc')\n",
|
"Test: delete general case with matches\n",
|
||||||
"node = linked_list.find('a')\n",
|
"Test: delete general case with no matches\n",
|
||||||
"print(node)\n",
|
"Success: test_delete\n",
|
||||||
"# Find in a list with no matches\n",
|
"\n",
|
||||||
"node = linked_list.find('aaa')\n",
|
"Test: len on an empty list\n",
|
||||||
"print(node)"
|
"Test: len general case\n",
|
||||||
|
"Success: test_len\n",
|
||||||
|
"\n"
|
||||||
]
|
]
|
||||||
},
|
}
|
||||||
{
|
],
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Test delete\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"# Delete in an empty list\n",
|
"\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"class Test(object):\n",
|
||||||
"linked_list.delete('a')\n",
|
" def test_insert_to_front(self):\n",
|
||||||
"linked_list.print_list()\n",
|
" print('Test: insert_to_front on an empty list')\n",
|
||||||
"# Delete a NULL\n",
|
" linked_list = LinkedList(None)\n",
|
||||||
"head = Node(10)\n",
|
" linked_list.insert_to_front(10)\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"linked_list.delete(None)\n",
|
"\n",
|
||||||
"linked_list.print_list()\n",
|
" print('Test: insert_to_front on a NULL')\n",
|
||||||
"# Delete in a list with one element or more matching elements\n",
|
" linked_list.insert_to_front(None)\n",
|
||||||
"head = Node(10)\n",
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"\n",
|
||||||
"linked_list.insert_to_front('a')\n",
|
" print('Test: insert_to_front general case')\n",
|
||||||
"linked_list.insert_to_front('bc')\n",
|
" linked_list.insert_to_front('a')\n",
|
||||||
"linked_list.delete('a')\n",
|
" linked_list.insert_to_front('bc')\n",
|
||||||
"linked_list.print_list()\n",
|
" assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
|
||||||
"# Delete in a list with no matches\n",
|
" \n",
|
||||||
"linked_list.delete('aa')"
|
" print('Success: test_insert_to_front\\n')\n",
|
||||||
]
|
" \n",
|
||||||
},
|
" def test_append(self):\n",
|
||||||
{
|
" print('Test: append on an empty list')\n",
|
||||||
"cell_type": "code",
|
" linked_list = LinkedList(None)\n",
|
||||||
"execution_count": null,
|
" linked_list.append(10)\n",
|
||||||
"metadata": {
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"collapsed": false
|
"\n",
|
||||||
},
|
" print('Test: append a NULL')\n",
|
||||||
"outputs": [],
|
" linked_list.append(None)\n",
|
||||||
"source": [
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"# Test length\n",
|
"\n",
|
||||||
"# Zero elements\n",
|
" print('Test: append general case')\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
" linked_list.append('a')\n",
|
||||||
"print(len(linked_list))\n",
|
" linked_list.append('bc')\n",
|
||||||
"# One or more elements\n",
|
" assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
|
||||||
"head = Node(10)\n",
|
" \n",
|
||||||
"linked_list = LinkedList(head)\n",
|
" print('Success: test_append\\n')\n",
|
||||||
"linked_list.insert_to_front('a')\n",
|
" \n",
|
||||||
"linked_list.insert_to_front('bc')\n",
|
" def test_find(self):\n",
|
||||||
"print(len(linked_list))"
|
" print('Test: find on an empty list')\n",
|
||||||
]
|
" linked_list = LinkedList(None)\n",
|
||||||
},
|
" node = linked_list.find('a')\n",
|
||||||
{
|
" assert_equal(node, None)\n",
|
||||||
"cell_type": "code",
|
"\n",
|
||||||
"execution_count": null,
|
" print('Test: find a NULL')\n",
|
||||||
"metadata": {
|
" head = Node(10)\n",
|
||||||
"collapsed": false
|
" linked_list = LinkedList(head)\n",
|
||||||
},
|
" node = linked_list.find(None)\n",
|
||||||
"outputs": [],
|
" assert_equal(node, None)\n",
|
||||||
"source": [
|
"\n",
|
||||||
"# Test empty print\n",
|
" print('Test: find general case with matches')\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
" head = Node(10)\n",
|
||||||
"linked_list.print_list()"
|
" linked_list = LinkedList(head)\n",
|
||||||
|
" linked_list.insert_to_front('a')\n",
|
||||||
|
" linked_list.insert_to_front('bc')\n",
|
||||||
|
" node = linked_list.find('a')\n",
|
||||||
|
" assert_equal(str(node), 'a')\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: find general case with no matches')\n",
|
||||||
|
" node = linked_list.find('aaa')\n",
|
||||||
|
" assert_equal(node, None)\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_find\\n')\n",
|
||||||
|
" \n",
|
||||||
|
" def test_delete(self):\n",
|
||||||
|
" print('Test: delete on an empty list')\n",
|
||||||
|
" linked_list = LinkedList(None)\n",
|
||||||
|
" linked_list.delete('a')\n",
|
||||||
|
" assert_equal(linked_list.get_all_data(), [])\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: delete a NULL')\n",
|
||||||
|
" head = Node(10)\n",
|
||||||
|
" linked_list = LinkedList(head)\n",
|
||||||
|
" linked_list.delete(None)\n",
|
||||||
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: delete general case with matches')\n",
|
||||||
|
" head = Node(10)\n",
|
||||||
|
" linked_list = LinkedList(head)\n",
|
||||||
|
" linked_list.insert_to_front('a')\n",
|
||||||
|
" linked_list.insert_to_front('bc')\n",
|
||||||
|
" linked_list.delete('a')\n",
|
||||||
|
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: delete general case with no matches')\n",
|
||||||
|
" linked_list.delete('aa')\n",
|
||||||
|
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_delete\\n')\n",
|
||||||
|
" \n",
|
||||||
|
" def test_len(self):\n",
|
||||||
|
" print('Test: len on an empty list')\n",
|
||||||
|
" linked_list = LinkedList(None)\n",
|
||||||
|
" assert_equal(len(linked_list), 0)\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: len general case')\n",
|
||||||
|
" head = Node(10)\n",
|
||||||
|
" linked_list = LinkedList(head)\n",
|
||||||
|
" linked_list.insert_to_front('a')\n",
|
||||||
|
" linked_list.insert_to_front('bc')\n",
|
||||||
|
" assert_equal(len(linked_list), 3)\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_len\\n')\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_insert_to_front()\n",
|
||||||
|
" test.test_append()\n",
|
||||||
|
" test.test_find()\n",
|
||||||
|
" test.test_delete()\n",
|
||||||
|
" test.test_len()"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
@ -28,6 +28,7 @@ class LinkedList(object):
|
||||||
else:
|
else:
|
||||||
node.next = self.head
|
node.next = self.head
|
||||||
self.head = node
|
self.head = node
|
||||||
|
return node
|
||||||
|
|
||||||
def append(self, data, next_node=None):
|
def append(self, data, next_node=None):
|
||||||
if data is None:
|
if data is None:
|
||||||
|
@ -40,6 +41,7 @@ class LinkedList(object):
|
||||||
while curr_node.next is not None:
|
while curr_node.next is not None:
|
||||||
curr_node = curr_node.next
|
curr_node = curr_node.next
|
||||||
curr_node.next = node
|
curr_node.next = node
|
||||||
|
return node
|
||||||
|
|
||||||
def find(self, data):
|
def find(self, data):
|
||||||
if data is None:
|
if data is None:
|
||||||
|
@ -52,6 +54,7 @@ class LinkedList(object):
|
||||||
return curr_node
|
return curr_node
|
||||||
else:
|
else:
|
||||||
curr_node = curr_node.next
|
curr_node = curr_node.next
|
||||||
|
return
|
||||||
|
|
||||||
def delete(self, data):
|
def delete(self, data):
|
||||||
if data is None:
|
if data is None:
|
||||||
|
@ -73,3 +76,11 @@ class LinkedList(object):
|
||||||
while curr_node is not None:
|
while curr_node is not None:
|
||||||
print(curr_node.data)
|
print(curr_node.data)
|
||||||
curr_node = curr_node.next
|
curr_node = curr_node.next
|
||||||
|
|
||||||
|
def get_all_data(self):
|
||||||
|
data = []
|
||||||
|
curr_node = self.head
|
||||||
|
while curr_node is not None:
|
||||||
|
data.append(curr_node.data)
|
||||||
|
curr_node = curr_node.next
|
||||||
|
return data
|
Loading…
Reference in New Issue
Block a user