mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
279 lines
7.9 KiB
Plaintext
279 lines
7.9 KiB
Plaintext
{
|
|
"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 queue with enqueue and dequeue methods using a linked list.\n",
|
|
"\n",
|
|
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
|
"* [Test Cases](#Test-Cases)\n",
|
|
"* [Algorithm](#Algorithm)\n",
|
|
"* [Code](#Code)\n",
|
|
"* [Unit Test](#Unit-Test)\n",
|
|
"* [Pythonic-Code](#Pythonic-Code)"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\n",
|
|
" * Yes\n",
|
|
"* If there are no items on the list, do you expect the first and last pointers to be NULL?\n",
|
|
" * Yes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Test Cases\n",
|
|
"\n",
|
|
"### Enqueue\n",
|
|
"\n",
|
|
"* Enqueue to an empty queue\n",
|
|
"* Enqueue to a non-empty queue\n",
|
|
"\n",
|
|
"### Dequeue\n",
|
|
"\n",
|
|
"* Dequeue an empty queue\n",
|
|
"* Dequeue a queue with one element\n",
|
|
"* Dequeue a queue with more than one element"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm\n",
|
|
"\n",
|
|
"### Enqueue\n",
|
|
"\n",
|
|
"* If the list is empty, set first and last to node\n",
|
|
"* Else, set last to node\n",
|
|
"\n",
|
|
"Complexity:\n",
|
|
"* Time: O(1)\n",
|
|
"* Space: O(1)\n",
|
|
"\n",
|
|
"### Dequeue\n",
|
|
"\n",
|
|
"* If the list is empty, return NULL\n",
|
|
"* If the list has one node\n",
|
|
" * Save the first node's value\n",
|
|
" * Set first and last to NULL\n",
|
|
" * Return the saved value\n",
|
|
"* Else\n",
|
|
" * Save the first node's value\n",
|
|
" * Set first to its next node\n",
|
|
" * Return the saved value\n",
|
|
"\n",
|
|
"Complexity:\n",
|
|
"* Time: O(1)\n",
|
|
"* Space: O(1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Overwriting queue_list.py\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%writefile queue_list.py\n",
|
|
"# Need to avoid naming this queue.py as it \n",
|
|
"# will conflict with IPython Notebook\n",
|
|
"\n",
|
|
"class Node(object):\n",
|
|
" def __init__(self, data):\n",
|
|
" self.data = data\n",
|
|
" self.next = None\n",
|
|
"\n",
|
|
"class Queue(object):\n",
|
|
" def __init__(self):\n",
|
|
" self.first = None\n",
|
|
" self.last = None\n",
|
|
"\n",
|
|
" def enqueue(self, data):\n",
|
|
" node = Node(data)\n",
|
|
" if self.first is None and self.last is None:\n",
|
|
" self.first = node\n",
|
|
" self.last = node\n",
|
|
" else:\n",
|
|
" self.last.next = node\n",
|
|
" self.last = node\n",
|
|
"\n",
|
|
" def dequeue(self):\n",
|
|
" # Empty list\n",
|
|
" if self.first is None and self.last is None:\n",
|
|
" return None\n",
|
|
" \n",
|
|
" # Remove only element from a one element list\n",
|
|
" elif self.first == self.last:\n",
|
|
" data = self.first.data\n",
|
|
" self.first = None\n",
|
|
" self.last = None\n",
|
|
" return data\n",
|
|
" else:\n",
|
|
" data = self.first.data\n",
|
|
" self.first = self.first.next\n",
|
|
" return data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%run queue_list.py"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Unit Test\n",
|
|
"\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.*"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Test: Dequeue an empty queue\n",
|
|
"Test: Enqueue to an empty queue\n",
|
|
"Test: Dequeue a queue with one element\n",
|
|
"Test: Enqueue to a non-empty queue\n",
|
|
"Test: Dequeue a queue with more than one element\n",
|
|
"Success: test_end_to_end\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from nose.tools import assert_equal\n",
|
|
"\n",
|
|
"class Test(object):\n",
|
|
" # TODO: It would be better if we had unit tests for each\n",
|
|
" # method in addition to the following end-to-end test\n",
|
|
" def test_end_to_end(self):\n",
|
|
" print('Test: Dequeue an empty queue')\n",
|
|
" queue = Queue()\n",
|
|
" assert_equal(queue.dequeue(), None)\n",
|
|
"\n",
|
|
" print('Test: Enqueue to an empty queue')\n",
|
|
" queue.enqueue(1)\n",
|
|
"\n",
|
|
" print('Test: Dequeue a queue with one element')\n",
|
|
" assert_equal(queue.dequeue(), 1)\n",
|
|
"\n",
|
|
" print('Test: Enqueue to a non-empty queue')\n",
|
|
" queue.enqueue(2)\n",
|
|
" queue.enqueue(3)\n",
|
|
" queue.enqueue(4)\n",
|
|
"\n",
|
|
" print('Test: Dequeue a queue with more than one element')\n",
|
|
" assert_equal(queue.dequeue(), 2)\n",
|
|
" assert_equal(queue.dequeue(), 3)\n",
|
|
" assert_equal(queue.dequeue(), 4)\n",
|
|
" \n",
|
|
" print('Success: test_end_to_end')\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" test = Test()\n",
|
|
" test.test_end_to_end()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Pythonic-Code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Source: https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-queues\n",
|
|
"<pre>\n",
|
|
"It is possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).\n",
|
|
"\n",
|
|
"To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends. For example:\n",
|
|
"\n",
|
|
">>>\n",
|
|
">>> from collections import deque\n",
|
|
">>> queue = deque([\"Eric\", \"John\", \"Michael\"])\n",
|
|
">>> queue.append(\"Terry\") # Terry arrives\n",
|
|
">>> queue.append(\"Graham\") # Graham arrives\n",
|
|
">>> queue.popleft() # The first to arrive now leaves\n",
|
|
"'Eric'\n",
|
|
">>> queue.popleft() # The second to arrive now leaves\n",
|
|
"'John'\n",
|
|
">>> queue # Remaining queue in order of arrival\n",
|
|
"deque(['Michael', 'Terry', 'Graham'])\n",
|
|
"</pre>"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|