interactive-coding-challenges/linked-lists/linked-list.ipynb

473 lines
14 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement a linked list with insert, append, find, delete, length, and print methods\n",
"\n",
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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",
"* Is this a singly or doubly linked list?\n",
" * Singly\n",
"* Is this a circular list?\n",
" * No\n",
"* Do we keep track of the tail or just the head?\n",
" * Just the head"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"### Insert to Front\n",
"\n",
"* Insert a NULL\n",
"* Insert in an empty list\n",
"* Insert in a list with one element or more elements\n",
"\n",
"### Append\n",
"\n",
"* Append a NULL\n",
"* Append in an empty list\n",
"* Insert in a list with one element or more elements\n",
"\n",
"### Find\n",
"\n",
2015-05-06 05:47:37 +08:00
"* Find a NULL\n",
"* Find in an empty list\n",
"* Find in a list with one element or more matching elements\n",
"* Find in a list with no matches\n",
"\n",
"### Delete\n",
"\n",
2015-05-09 04:49:33 +08:00
"* Delete a NULL\n",
"* Delete in an empty list\n",
"* Delete in a list with one element or more matching elements\n",
"* Delete in a list with no matches\n",
"\n",
"### Length\n",
"\n",
"* Length of zero or more elements\n",
"\n",
"### Print\n",
"\n",
"* Print an empty list\n",
"* Print a list with one or more elements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"### Insert to Front\n",
"\n",
"* If the data we are inserting is NULL, return\n",
"* Create a node with the input data\n",
"* If this is an empty list\n",
" * Assign the head to the node\n",
"* Else\n",
" * Set the node.next to the head\n",
" * Set the head to the node\n",
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(1), a new node is added\n",
"\n",
"### Append\n",
"\n",
"* If the data we are inserting is NULL, return\n",
"* Create a node with the input data\n",
"* If this is an empty list\n",
" * Assign the head to the node\n",
"* Else\n",
" * Iterate to the end of the list\n",
" * Set the final node's next to the new node\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(1), a new node is added\n",
"\n",
"### Find\n",
"\n",
2015-05-06 05:47:37 +08:00
"* If data we are finding is NULL, return\n",
"* If the list is empty, return\n",
"* For each node\n",
" * If the value is a match, return it\n",
" * Else, move on to the next node\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place\n",
"\n",
"### Delete\n",
"\n",
2015-05-09 04:49:33 +08:00
"* If data we are deleting is NULL, return\n",
"* If the list is empty, return\n",
"* For each node, keep track of previous and current node\n",
" * If the value we are deleting is a match in the current node\n",
" * Update previous node's next pointer to the current node's next pointer\n",
" * We do not have have to explicitly delete in Python\n",
" * Else, move on to the next node\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place\n",
"\n",
"### Length\n",
"\n",
"* For each node\n",
" * Increase length counter\n",
" \n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place\n",
"\n",
"### Print\n",
"\n",
"* For each node\n",
" * Print the node's value\n",
" \n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting linked_list.py\n"
]
}
],
"source": [
"%%writefile linked_list.py\n",
"\n",
"class Node(object):\n",
" def __init__(self, data, next_node=None):\n",
" self.next = next_node\n",
" self.data = data\n",
" \n",
" def __str__(self):\n",
" return self.data\n",
"\n",
"class LinkedList(object):\n",
" def __init__(self, head=None):\n",
" self.head = head\n",
"\n",
" def __len__(self):\n",
" curr = self.head\n",
" counter = 0\n",
" while curr is not None:\n",
" counter += 1\n",
" curr = curr.next\n",
" return counter\n",
" \n",
" def insert_to_front(self, data):\n",
" if data is None:\n",
" return\n",
" node = Node(data)\n",
" if self.head is None:\n",
" self.head = node\n",
" else:\n",
" node.next = self.head\n",
" self.head = node\n",
" return node\n",
2015-05-06 05:47:37 +08:00
" \n",
" def append(self, data, next_node=None):\n",
" if data is None:\n",
" return\n",
" node = Node(data, next_node)\n",
" if self.head is None:\n",
" self.head = node\n",
" else:\n",
" curr_node = self.head\n",
" while curr_node.next is not None:\n",
" curr_node = curr_node.next\n",
" curr_node.next = node\n",
" return node\n",
" \n",
2015-05-06 05:47:37 +08:00
" def find(self, data):\n",
" if data is None:\n",
" return\n",
" if self.head is None:\n",
" return\n",
" curr_node = self.head\n",
" while curr_node is not None:\n",
" if curr_node.data == data:\n",
" return curr_node\n",
" else:\n",
" curr_node = curr_node.next\n",
" return\n",
2015-05-09 04:49:33 +08:00
" \n",
" def delete(self, data):\n",
" if data is None:\n",
" return\n",
" if self.head is None:\n",
" return\n",
" prev_node = self.head\n",
" curr_node = prev_node.next\n",
" while curr_node is not None:\n",
" if curr_node.data == data:\n",
" prev_node.next = curr_node.next\n",
" return\n",
" else:\n",
" prev_node = curr_node\n",
" curr_node = curr_node.next\n",
"\n",
" def print_list(self):\n",
" curr_node = self.head\n",
" while curr_node is not None:\n",
" print(curr_node.data)\n",
" 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",
"execution_count": 2,
2015-05-06 05:47:37 +08:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%run linked_list.py"
2015-05-06 05:47:37 +08:00
]
},
2015-05-09 04:49:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
2015-05-09 04:49:33 +08:00
"source": [
"## Unit Test"
2015-05-09 04:49:33 +08:00
]
},
{
"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.*"
]
},
2015-05-06 05:47:37 +08:00
{
"cell_type": "code",
"execution_count": 3,
"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": [
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_insert_to_front(self):\n",
" print('Test: insert_to_front on an empty list')\n",
" linked_list = LinkedList(None)\n",
" linked_list.insert_to_front(10)\n",
" assert_equal(linked_list.get_all_data(), [10])\n",
"\n",
" print('Test: insert_to_front on a NULL')\n",
" linked_list.insert_to_front(None)\n",
" 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",
" linked_list.insert_to_front('bc')\n",
" assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
" \n",
" print('Success: test_insert_to_front\\n')\n",
" \n",
" def test_append(self):\n",
" print('Test: append on an empty list')\n",
" linked_list = LinkedList(None)\n",
" linked_list.append(10)\n",
" assert_equal(linked_list.get_all_data(), [10])\n",
"\n",
" print('Test: append a NULL')\n",
" linked_list.append(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n",
"\n",
" print('Test: append general case')\n",
" linked_list.append('a')\n",
" linked_list.append('bc')\n",
" assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
" \n",
" print('Success: test_append\\n')\n",
" \n",
" def test_find(self):\n",
" print('Test: find on an empty list')\n",
" linked_list = LinkedList(None)\n",
" node = linked_list.find('a')\n",
" assert_equal(node, None)\n",
"\n",
" print('Test: find a NULL')\n",
" head = Node(10)\n",
" linked_list = LinkedList(head)\n",
" node = linked_list.find(None)\n",
" assert_equal(node, None)\n",
"\n",
" print('Test: find 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",
" 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()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}