Polish queue challenge and solution (#70)

Update constraints, algorithm discussion, and code.
This commit is contained in:
Donne Martin 2016-06-19 20:08:26 -04:00 committed by GitHub
parent 4cdb85e22d
commit 0d20ff5931
3 changed files with 46 additions and 40 deletions

View File

@ -8,27 +8,28 @@ class Node(object):
class Queue(object): class Queue(object):
def __init__(self): def __init__(self):
self.first = None self.head = None
self.last = None self.tail = None
def enqueue(self, data): def enqueue(self, data):
node = Node(data) node = Node(data)
if self.first is None and self.last is None: # Empty list
self.first = node if self.head is None and self.tail is None:
self.last = node self.head = node
self.tail = node
else: else:
self.last.next = node self.tail.next = node
self.last = node self.tail = node
def dequeue(self): def dequeue(self):
# Empty list # Empty list
if self.first is None and self.last is None: if self.head is None and self.tail is None:
return None return None
data = self.first.data data = self.head.data
# Remove only element from a one element list # Remove only element from a one element list
if self.first == self.last: if self.head == self.tail:
self.first = None self.head = None
self.last = None self.tail = None
else: else:
self.first = self.first.next self.head = self.head.next
return data return data

View File

@ -40,6 +40,8 @@
"* If there are no items on the list, do you expect the first and last pointers to be None?\n", "* If there are no items on the list, do you expect the first and last pointers to be None?\n",
" * Yes\n", " * Yes\n",
"* If you dequeue on an empty queue, does that return None?\n", "* If you dequeue on an empty queue, does that return None?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -179,21 +181,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -34,11 +34,13 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\n", "* If there is one item in the list, do you expect the head and tail pointers to both point to it?\n",
" * Yes\n", " * Yes\n",
"* If there are no items on the list, do you expect the first and last pointers to be None?\n", "* If there are no items on the list, do you expect the head and tail pointers to be None?\n",
" * Yes\n", " * Yes\n",
"* If you dequeue on an empty queue, does that return None?\n", "* If you dequeue on an empty queue, does that return None?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -68,8 +70,8 @@
"\n", "\n",
"### Enqueue\n", "### Enqueue\n",
"\n", "\n",
"* If the list is empty, set first and last to node\n", "* If the list is empty, set head and tail to node\n",
"* Else, set last to node\n", "* Else, set tail to node\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(1)\n", "* Time: O(1)\n",
@ -79,12 +81,12 @@
"\n", "\n",
"* If the list is empty, return None\n", "* If the list is empty, return None\n",
"* If the list has one node\n", "* If the list has one node\n",
" * Save the first node's value\n", " * Save the head node's value\n",
" * Set first and last to None\n", " * Set head and tail to None\n",
" * Return the saved value\n", " * Return the saved value\n",
"* Else\n", "* Else\n",
" * Save the first node's value\n", " * Save the head node's value\n",
" * Set first to its next node\n", " * Set head to its next node\n",
" * Return the saved value\n", " * Return the saved value\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
@ -126,29 +128,30 @@
"class Queue(object):\n", "class Queue(object):\n",
"\n", "\n",
" def __init__(self):\n", " def __init__(self):\n",
" self.first = None\n", " self.head = None\n",
" self.last = None\n", " self.tail = None\n",
"\n", "\n",
" def enqueue(self, data):\n", " def enqueue(self, data):\n",
" node = Node(data)\n", " node = Node(data)\n",
" if self.first is None and self.last is None:\n", " # Empty list\n",
" self.first = node\n", " if self.head is None and self.tail is None:\n",
" self.last = node\n", " self.head = node\n",
" self.tail = node\n",
" else:\n", " else:\n",
" self.last.next = node\n", " self.tail.next = node\n",
" self.last = node\n", " self.tail = node\n",
"\n", "\n",
" def dequeue(self):\n", " def dequeue(self):\n",
" # Empty list\n", " # Empty list\n",
" if self.first is None and self.last is None:\n", " if self.head is None and self.tail is None:\n",
" return None\n", " return None\n",
" data = self.first.data\n", " data = self.head.data\n",
" # Remove only element from a one element list\n", " # Remove only element from a one element list\n",
" if self.first == self.last:\n", " if self.head == self.tail:\n",
" self.first = None\n", " self.head = None\n",
" self.last = None\n", " self.tail = None\n",
" else:\n", " else:\n",
" self.first = self.first.next\n", " self.head = self.head.next\n",
" return data" " return data"
] ]
}, },
@ -300,7 +303,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,