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": [
|
||||||
|
{
|
||||||
|
"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": [
|
"source": [
|
||||||
"# Test append\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"# Insert in an empty list\n",
|
"\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"class Test(object):\n",
|
||||||
"linked_list.append(10)\n",
|
" def test_insert_to_front(self):\n",
|
||||||
"linked_list.print_list()\n",
|
" print('Test: insert_to_front on an empty list')\n",
|
||||||
"# Insert a NULL\n",
|
" linked_list = LinkedList(None)\n",
|
||||||
"linked_list.append(None)\n",
|
" linked_list.insert_to_front(10)\n",
|
||||||
"linked_list.print_list()\n",
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"# Insert in a list with one element or more elements\n",
|
"\n",
|
||||||
"linked_list.append('a')\n",
|
" print('Test: insert_to_front on a NULL')\n",
|
||||||
"linked_list.append('bc')\n",
|
" linked_list.insert_to_front(None)\n",
|
||||||
"linked_list.print_list()"
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
]
|
"\n",
|
||||||
},
|
" print('Test: insert_to_front general case')\n",
|
||||||
{
|
" linked_list.insert_to_front('a')\n",
|
||||||
"cell_type": "code",
|
" linked_list.insert_to_front('bc')\n",
|
||||||
"execution_count": null,
|
" assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
|
||||||
"metadata": {
|
" \n",
|
||||||
"collapsed": false
|
" print('Success: test_insert_to_front\\n')\n",
|
||||||
},
|
" \n",
|
||||||
"outputs": [],
|
" def test_append(self):\n",
|
||||||
"source": [
|
" print('Test: append on an empty list')\n",
|
||||||
"# Test find\n",
|
" linked_list = LinkedList(None)\n",
|
||||||
"# Find in an empty list\n",
|
" linked_list.append(10)\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"node = linked_list.find('a')\n",
|
"\n",
|
||||||
"print(node)\n",
|
" print('Test: append a NULL')\n",
|
||||||
"# Find a NULL\n",
|
" linked_list.append(None)\n",
|
||||||
"head = Node(10)\n",
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"\n",
|
||||||
"node = linked_list.find(None)\n",
|
" print('Test: append general case')\n",
|
||||||
"print(node)\n",
|
" linked_list.append('a')\n",
|
||||||
"# Find in a list with one element or more matching elements\n",
|
" linked_list.append('bc')\n",
|
||||||
"head = Node(10)\n",
|
" assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
" \n",
|
||||||
"linked_list.insert_to_front('a')\n",
|
" print('Success: test_append\\n')\n",
|
||||||
"linked_list.insert_to_front('bc')\n",
|
" \n",
|
||||||
"node = linked_list.find('a')\n",
|
" def test_find(self):\n",
|
||||||
"print(node)\n",
|
" print('Test: find on an empty list')\n",
|
||||||
"# Find in a list with no matches\n",
|
" linked_list = LinkedList(None)\n",
|
||||||
"node = linked_list.find('aaa')\n",
|
" node = linked_list.find('a')\n",
|
||||||
"print(node)"
|
" assert_equal(node, None)\n",
|
||||||
]
|
"\n",
|
||||||
},
|
" print('Test: find a NULL')\n",
|
||||||
{
|
" head = Node(10)\n",
|
||||||
"cell_type": "code",
|
" linked_list = LinkedList(head)\n",
|
||||||
"execution_count": null,
|
" node = linked_list.find(None)\n",
|
||||||
"metadata": {
|
" assert_equal(node, None)\n",
|
||||||
"collapsed": false
|
"\n",
|
||||||
},
|
" print('Test: find general case with matches')\n",
|
||||||
"outputs": [],
|
" head = Node(10)\n",
|
||||||
"source": [
|
" linked_list = LinkedList(head)\n",
|
||||||
"# Test delete\n",
|
" linked_list.insert_to_front('a')\n",
|
||||||
"# Delete in an empty list\n",
|
" linked_list.insert_to_front('bc')\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
" node = linked_list.find('a')\n",
|
||||||
"linked_list.delete('a')\n",
|
" assert_equal(str(node), 'a')\n",
|
||||||
"linked_list.print_list()\n",
|
"\n",
|
||||||
"# Delete a NULL\n",
|
" print('Test: find general case with no matches')\n",
|
||||||
"head = Node(10)\n",
|
" node = linked_list.find('aaa')\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
" assert_equal(node, None)\n",
|
||||||
"linked_list.delete(None)\n",
|
" \n",
|
||||||
"linked_list.print_list()\n",
|
" print('Success: test_find\\n')\n",
|
||||||
"# Delete in a list with one element or more matching elements\n",
|
" \n",
|
||||||
"head = Node(10)\n",
|
" def test_delete(self):\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
" print('Test: delete on an empty list')\n",
|
||||||
"linked_list.insert_to_front('a')\n",
|
" linked_list = LinkedList(None)\n",
|
||||||
"linked_list.insert_to_front('bc')\n",
|
" linked_list.delete('a')\n",
|
||||||
"linked_list.delete('a')\n",
|
" assert_equal(linked_list.get_all_data(), [])\n",
|
||||||
"linked_list.print_list()\n",
|
"\n",
|
||||||
"# Delete in a list with no matches\n",
|
" print('Test: delete a NULL')\n",
|
||||||
"linked_list.delete('aa')"
|
" head = Node(10)\n",
|
||||||
]
|
" linked_list = LinkedList(head)\n",
|
||||||
},
|
" linked_list.delete(None)\n",
|
||||||
{
|
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||||
"cell_type": "code",
|
"\n",
|
||||||
"execution_count": null,
|
" print('Test: delete general case with matches')\n",
|
||||||
"metadata": {
|
" head = Node(10)\n",
|
||||||
"collapsed": false
|
" linked_list = LinkedList(head)\n",
|
||||||
},
|
" linked_list.insert_to_front('a')\n",
|
||||||
"outputs": [],
|
" linked_list.insert_to_front('bc')\n",
|
||||||
"source": [
|
" linked_list.delete('a')\n",
|
||||||
"# Test length\n",
|
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
|
||||||
"# Zero elements\n",
|
"\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
" print('Test: delete general case with no matches')\n",
|
||||||
"print(len(linked_list))\n",
|
" linked_list.delete('aa')\n",
|
||||||
"# One or more elements\n",
|
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
|
||||||
"head = Node(10)\n",
|
" \n",
|
||||||
"linked_list = LinkedList(head)\n",
|
" print('Success: test_delete\\n')\n",
|
||||||
"linked_list.insert_to_front('a')\n",
|
" \n",
|
||||||
"linked_list.insert_to_front('bc')\n",
|
" def test_len(self):\n",
|
||||||
"print(len(linked_list))"
|
" print('Test: len on an empty list')\n",
|
||||||
]
|
" linked_list = LinkedList(None)\n",
|
||||||
},
|
" assert_equal(len(linked_list), 0)\n",
|
||||||
{
|
"\n",
|
||||||
"cell_type": "code",
|
" print('Test: len general case')\n",
|
||||||
"execution_count": null,
|
" head = Node(10)\n",
|
||||||
"metadata": {
|
" linked_list = LinkedList(head)\n",
|
||||||
"collapsed": false
|
" linked_list.insert_to_front('a')\n",
|
||||||
},
|
" linked_list.insert_to_front('bc')\n",
|
||||||
"outputs": [],
|
" assert_equal(len(linked_list), 3)\n",
|
||||||
"source": [
|
" \n",
|
||||||
"# Test empty print\n",
|
" print('Success: test_len\\n')\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"\n",
|
||||||
"linked_list.print_list()"
|
"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:
|
||||||
|
@ -72,4 +75,12 @@ class LinkedList(object):
|
||||||
curr_node = self.head
|
curr_node = self.head
|
||||||
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