mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Refactored linked list class. Class is now loaded by other notebooks that reference it.
This commit is contained in:
parent
6f3e3b0b8f
commit
4c1f87a2ae
|
@ -19,8 +19,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Clarifying Questions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Do we assume the linked list class already exists?\n",
|
|
||||||
" * No, create one\n",
|
|
||||||
"* What if the final node is being deleted, for example a single node list?\n",
|
"* What if the final node is being deleted, for example a single node list?\n",
|
||||||
" * Make it a dummy node"
|
" * Make it a dummy node"
|
||||||
]
|
]
|
||||||
|
@ -61,6 +59,17 @@
|
||||||
"## Code"
|
"## Code"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run linked_list.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -74,10 +83,7 @@
|
||||||
" self.data = data\n",
|
" self.data = data\n",
|
||||||
" self.next = None\n",
|
" self.next = None\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class LinkedList(object):\n",
|
"class MyLinkedList(LinkedList):\n",
|
||||||
" def __init__(self, head):\n",
|
|
||||||
" self.head = head\n",
|
|
||||||
" \n",
|
|
||||||
" def delete_node(self, node):\n",
|
" def delete_node(self, node):\n",
|
||||||
" if self.head is None:\n",
|
" if self.head is None:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
|
@ -89,25 +95,7 @@
|
||||||
" node.next = None\n",
|
" node.next = None\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" node.data = next.data\n",
|
" node.data = next.data\n",
|
||||||
" node.next = next.next\n",
|
" node.next = next.next"
|
||||||
"\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",
|
|
||||||
" \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"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,17 +18,13 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Clarifying Questions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Do we assume the linked list class already exists?\n",
|
|
||||||
" * No, create one\n",
|
|
||||||
"* Is k an integer?\n",
|
"* Is k an integer?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* If k = 0, does this return the last element?\n",
|
"* If k = 0, does this return the last element?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* What happens if k is greater than or equal to the length of the linked list?\n",
|
"* What happens if k is greater than or equal to the length of the linked list?\n",
|
||||||
" * Return None\n",
|
" * Return None\n",
|
||||||
"* Can you insert NULL values in the list?\n",
|
"* Can you use additional data structures?\n",
|
||||||
" * No\n",
|
|
||||||
"* Can use additional data structures?\n",
|
|
||||||
" * No"
|
" * No"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -69,6 +65,17 @@
|
||||||
"## Code"
|
"## Code"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run linked_list.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -77,28 +84,12 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class MyLinkedList(LinkedList):\n",
|
||||||
" def __init__(self, data):\n",
|
|
||||||
" self.data = data\n",
|
|
||||||
" self.next = None\n",
|
|
||||||
" \n",
|
|
||||||
"class LinkedList(object):\n",
|
|
||||||
" def __init__(self, head):\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 kth_to_last_elem(self, k):\n",
|
" def kth_to_last_elem(self, k):\n",
|
||||||
" if self.head is None:\n",
|
" if self.head is None:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" if type(k) != int:\n",
|
" if type(k) != int:\n",
|
||||||
" return\n",
|
" raise ValueError('')\n",
|
||||||
" if k >= len(self):\n",
|
" if k >= len(self):\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" curr = self.head\n",
|
" curr = self.head\n",
|
||||||
|
@ -112,20 +103,19 @@
|
||||||
" while curr.next is not None:\n",
|
" while curr.next is not None:\n",
|
||||||
" curr = curr.next\n",
|
" curr = curr.next\n",
|
||||||
" prev = prev.next\n",
|
" prev = prev.next\n",
|
||||||
" return prev.data\n",
|
" return prev.data"
|
||||||
" \n",
|
]
|
||||||
" def insert_to_front(self, data):\n",
|
},
|
||||||
" if data is None:\n",
|
{
|
||||||
" return\n",
|
"cell_type": "code",
|
||||||
" node = Node(data)\n",
|
"execution_count": null,
|
||||||
" if self.head is None:\n",
|
"metadata": {
|
||||||
" self.head = node\n",
|
"collapsed": false
|
||||||
" else:\n",
|
},
|
||||||
" node.next = self.head\n",
|
"outputs": [],
|
||||||
" self.head = node\n",
|
"source": [
|
||||||
" \n",
|
|
||||||
"# Empty list\n",
|
"# Empty list\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"linked_list = MyLinkedList(None)\n",
|
||||||
"print(linked_list.kth_to_last_elem(0))\n",
|
"print(linked_list.kth_to_last_elem(0))\n",
|
||||||
"# k is not an integer\n",
|
"# k is not an integer\n",
|
||||||
"print(linked_list.kth_to_last_elem('a'))\n",
|
"print(linked_list.kth_to_last_elem('a'))\n",
|
||||||
|
@ -133,7 +123,7 @@
|
||||||
"print(linked_list.kth_to_last_elem(100))\n",
|
"print(linked_list.kth_to_last_elem(100))\n",
|
||||||
"# One element, k = 0\n",
|
"# One element, k = 0\n",
|
||||||
"head = Node(2)\n",
|
"head = Node(2)\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"linked_list = MyLinkedList(head)\n",
|
||||||
"print(linked_list.kth_to_last_elem(0))\n",
|
"print(linked_list.kth_to_last_elem(0))\n",
|
||||||
"# General case with many elements, k < length of linked list\n",
|
"# General case with many elements, k < length of linked list\n",
|
||||||
"linked_list.insert_to_front(1)\n",
|
"linked_list.insert_to_front(1)\n",
|
||||||
|
|
|
@ -20,13 +20,11 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Clarifying Questions\n",
|
"## Clarifying Questions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Do we assume the linked list class already exists?\n",
|
|
||||||
" * No, create one\n",
|
|
||||||
"* Is this a singly or doubly linked list?\n",
|
"* Is this a singly or doubly linked list?\n",
|
||||||
" * Singly\n",
|
" * Singly\n",
|
||||||
"* Can you insert NULL values in the list?\n",
|
"* Can you insert NULL values in the list?\n",
|
||||||
" * No\n",
|
" * No\n",
|
||||||
"* Can use additional data structures?\n",
|
"* Can you use additional data structures?\n",
|
||||||
" * Implement both solutions"
|
" * Implement both solutions"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -70,6 +68,17 @@
|
||||||
"## Code: Hash Map Lookup"
|
"## Code: Hash Map Lookup"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run linked_list.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -78,15 +87,7 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class MyLinkedList(LinkedList):\n",
|
||||||
" def __init__(self, data):\n",
|
|
||||||
" self.next = None\n",
|
|
||||||
" self.data = data\n",
|
|
||||||
"\n",
|
|
||||||
"class LinkedList(object):\n",
|
|
||||||
" def __init__(self, head):\n",
|
|
||||||
" self.head = head\n",
|
|
||||||
" \n",
|
|
||||||
" def remove_dupes(self):\n",
|
" def remove_dupes(self):\n",
|
||||||
" seen_data = set()\n",
|
" seen_data = set()\n",
|
||||||
" curr = self.head\n",
|
" curr = self.head\n",
|
||||||
|
@ -97,31 +98,24 @@
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" seen_data.add(curr.data)\n",
|
" seen_data.add(curr.data)\n",
|
||||||
" prev = curr\n",
|
" prev = curr\n",
|
||||||
" curr = curr.next\n",
|
" curr = curr.next"
|
||||||
" \n",
|
]
|
||||||
" def insert_to_front(self, data):\n",
|
},
|
||||||
" if data is None:\n",
|
{
|
||||||
" return\n",
|
"cell_type": "code",
|
||||||
" node = Node(data)\n",
|
"execution_count": null,
|
||||||
" if self.head is None:\n",
|
"metadata": {
|
||||||
" self.head = node\n",
|
"collapsed": false
|
||||||
" else:\n",
|
},
|
||||||
" node.next = self.head\n",
|
"outputs": [],
|
||||||
" self.head = node \n",
|
"source": [
|
||||||
" \n",
|
|
||||||
" def print_list(self):\n",
|
|
||||||
" curr = self.head\n",
|
|
||||||
" while curr is not None:\n",
|
|
||||||
" print(curr.data)\n",
|
|
||||||
" curr = curr.next\n",
|
|
||||||
" \n",
|
|
||||||
"# Empty linked list\n",
|
"# Empty linked list\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"linked_list = MyLinkedList(None)\n",
|
||||||
"linked_list.remove_dupes()\n",
|
"linked_list.remove_dupes()\n",
|
||||||
"linked_list.print_list()\n",
|
"linked_list.print_list()\n",
|
||||||
"# One element linked list\n",
|
"# One element linked list\n",
|
||||||
"head = Node(2)\n",
|
"head = Node(2)\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"linked_list = MyLinkedList(head)\n",
|
||||||
"linked_list.remove_dupes()\n",
|
"linked_list.remove_dupes()\n",
|
||||||
"linked_list.print_list()\n",
|
"linked_list.print_list()\n",
|
||||||
"# Multiple elements\n",
|
"# Multiple elements\n",
|
||||||
|
@ -174,14 +168,7 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class MyLinkedList(LinkedList):\n",
|
||||||
" def __init__(self, data):\n",
|
|
||||||
" self.next = None\n",
|
|
||||||
" self.data = data\n",
|
|
||||||
"\n",
|
|
||||||
"class LinkedList(object):\n",
|
|
||||||
" def __init__(self, head):\n",
|
|
||||||
" self.head = head\n",
|
|
||||||
" \n",
|
" \n",
|
||||||
" def remove_dupes(self):\n",
|
" def remove_dupes(self):\n",
|
||||||
" curr = self.head\n",
|
" curr = self.head\n",
|
||||||
|
@ -192,31 +179,24 @@
|
||||||
" runner.next = runner.next.next\n",
|
" runner.next = runner.next.next\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" runner = runner.next\n",
|
" runner = runner.next\n",
|
||||||
" curr = curr.next\n",
|
" curr = curr.next"
|
||||||
" \n",
|
]
|
||||||
" def insert_to_front(self, data):\n",
|
},
|
||||||
" if data is None:\n",
|
{
|
||||||
" return\n",
|
"cell_type": "code",
|
||||||
" node = Node(data)\n",
|
"execution_count": null,
|
||||||
" if self.head is None:\n",
|
"metadata": {
|
||||||
" self.head = node\n",
|
"collapsed": true
|
||||||
" else:\n",
|
},
|
||||||
" node.next = self.head\n",
|
"outputs": [],
|
||||||
" self.head = node \n",
|
"source": [
|
||||||
" \n",
|
|
||||||
" def print_list(self):\n",
|
|
||||||
" curr = self.head\n",
|
|
||||||
" while curr is not None:\n",
|
|
||||||
" print(curr.data)\n",
|
|
||||||
" curr = curr.next\n",
|
|
||||||
" \n",
|
|
||||||
"# Empty linked list\n",
|
"# Empty linked list\n",
|
||||||
"linked_list = LinkedList(None)\n",
|
"linked_list = MyLinkedList(None)\n",
|
||||||
"linked_list.remove_dupes()\n",
|
"linked_list.remove_dupes()\n",
|
||||||
"linked_list.print_list()\n",
|
"linked_list.print_list()\n",
|
||||||
"# One element linked list\n",
|
"# One element linked list\n",
|
||||||
"head = Node(2)\n",
|
"head = Node(2)\n",
|
||||||
"linked_list = LinkedList(head)\n",
|
"linked_list = MyLinkedList(head)\n",
|
||||||
"linked_list.remove_dupes()\n",
|
"linked_list.remove_dupes()\n",
|
||||||
"linked_list.print_list()\n",
|
"linked_list.print_list()\n",
|
||||||
"# Multiple elements\n",
|
"# Multiple elements\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user