mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Updated problem solution to use stack's is_empty() method rather than a similar method that used to be in QueueFromStacks.
This commit is contained in:
parent
273d0efcb4
commit
9fefcd12f0
|
@ -85,7 +85,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": true
|
||||||
},
|
},
|
||||||
|
@ -96,7 +96,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -107,52 +107,28 @@
|
||||||
" self.left_stack = Stack()\n",
|
" self.left_stack = Stack()\n",
|
||||||
" self.right_stack = Stack()\n",
|
" self.right_stack = Stack()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def is_stack_empty(self, stack):\n",
|
|
||||||
" return stack.peek() is None\n",
|
|
||||||
"\n",
|
|
||||||
" def shift_stacks(self, source, destination):\n",
|
" def shift_stacks(self, source, destination):\n",
|
||||||
" while source.peek() is not None:\n",
|
" while source.peek() is not None:\n",
|
||||||
" destination.push(source.pop())\n",
|
" destination.push(source.pop())\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def enqueue(self, data):\n",
|
" def enqueue(self, data):\n",
|
||||||
" if self.is_stack_empty(self.left_stack) and not self.is_stack_empty(self.right_stack):\n",
|
" if self.left_stack.is_empty() and not self.right_stack.is_empty():\n",
|
||||||
" self.shift_stacks(self.right_stack, self.left_stack)\n",
|
" self.shift_stacks(self.right_stack, self.left_stack)\n",
|
||||||
" self.left_stack.push(data)\n",
|
" self.left_stack.push(data)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def dequeue(self):\n",
|
" def dequeue(self):\n",
|
||||||
" if self.is_stack_empty(self.right_stack) and not self.is_stack_empty(self.left_stack):\n",
|
" if self.right_stack.is_empty() and not self.left_stack.is_empty():\n",
|
||||||
" self.shift_stacks(self.left_stack, self.right_stack)\n",
|
" self.shift_stacks(self.left_stack, self.right_stack)\n",
|
||||||
" return self.right_stack.pop()"
|
" return self.right_stack.pop()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 14,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Dequeue on empty stack\n",
|
|
||||||
"None\n",
|
|
||||||
"Enqueue on empty stack\n",
|
|
||||||
"Enqueue on non-empty stack\n",
|
|
||||||
"Multiple enqueue in a row\n",
|
|
||||||
"Dequeue on non-empty stack\n",
|
|
||||||
"Dequeue after an enqueue\n",
|
|
||||||
"0\n",
|
|
||||||
"Multiple dequeue in a row\n",
|
|
||||||
"1\n",
|
|
||||||
"2\n",
|
|
||||||
"Enqueue after a dequeue\n",
|
|
||||||
"5\n",
|
|
||||||
"None\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print('Dequeue on empty stack')\n",
|
"print('Dequeue on empty stack')\n",
|
||||||
"queue = QueueFromStacks()\n",
|
"queue = QueueFromStacks()\n",
|
||||||
|
@ -173,15 +149,6 @@
|
||||||
"queue.enqueue(5)\n",
|
"queue.enqueue(5)\n",
|
||||||
"print(queue.dequeue())"
|
"print(queue.dequeue())"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user