mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
255 lines
6.1 KiB
Plaintext
255 lines
6.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem: Implement a linked list with insert, find, delete, and print methods.\n",
|
|
"\n",
|
|
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
|
"* [Test Cases](#Test-Cases)\n",
|
|
"* [Algorithm](#Algorithm)\n",
|
|
"* [Code](#Code)\n",
|
|
"* [Pythonic-Code](#Pythonic-Code)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Clarifying Questions\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\n",
|
|
"* Can you insert NULL values in the list?\n",
|
|
" * No"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"### Find\n",
|
|
"\n",
|
|
"* 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",
|
|
"* \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",
|
|
"### Find\n",
|
|
"\n",
|
|
"* 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",
|
|
"* Coming soon\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": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class Node(object):\n",
|
|
" def __init__(self, data):\n",
|
|
" self.next = None\n",
|
|
" self.data = data\n",
|
|
" \n",
|
|
" def __str__(self):\n",
|
|
" return self.data\n",
|
|
"\n",
|
|
"class LinkedList(object):\n",
|
|
" def __init__(self, head):\n",
|
|
" self.head = head\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",
|
|
" \n",
|
|
" 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",
|
|
"\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Test insert_to_front\n",
|
|
"# Insert in an empty list\n",
|
|
"linked_list = LinkedList(None)\n",
|
|
"linked_list.insert_to_front(10)\n",
|
|
"linked_list.print_list()\n",
|
|
"# Insert a NULL\n",
|
|
"linked_list.insert_to_front(None)\n",
|
|
"linked_list.print_list()\n",
|
|
"# 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",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Test find\n",
|
|
"# Find a NULL\n",
|
|
"linked_list = LinkedList(None)\n",
|
|
"node = linked_list.find('a')\n",
|
|
"print(node)\n",
|
|
"# Find in an empty list\n",
|
|
"head = Node(10)\n",
|
|
"linked_list = LinkedList(None)\n",
|
|
"node = linked_list.find(None)\n",
|
|
"print(node)\n",
|
|
"# Find in a list with one element or more matching elements\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",
|
|
"print(node)\n",
|
|
"# Find in a list with no matches\n",
|
|
"node = linked_list.find('aaa')\n",
|
|
"print(node)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Test empty print\n",
|
|
"linked_list = LinkedList(None)\n",
|
|
"linked_list.print_list()"
|
|
]
|
|
}
|
|
],
|
|
"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.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|