mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish queue from stacks challenge and solution (#74)
Update constraints, algorithm discussion, and code.
This commit is contained in:
parent
7f338b0e64
commit
c7e9a85db7
|
@ -34,9 +34,13 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Do you expect the methods to be enqueue and dequeue?\n",
|
||||
"* Do we expect the methods to be enqueue and dequeue?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||
" * Yes\n",
|
||||
"* Can we push a None value to the Stack?\n",
|
||||
" * No\n",
|
||||
"* Can we assume this fits memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -179,21 +183,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -33,9 +33,13 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Do you expect the methods to be enqueue and dequeue?\n",
|
||||
"* Do we expect the methods to be enqueue and dequeue?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||
" * Yes\n",
|
||||
"* Can we push a None value to the Stack?\n",
|
||||
" * No\n",
|
||||
"* Can we assume this fits memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -65,7 +69,7 @@
|
|||
"\n",
|
||||
"### Enqueue\n",
|
||||
"\n",
|
||||
"* If the left stack is empty and the right stack is not empty\n",
|
||||
"* If right stack is not empty\n",
|
||||
" * Shift the elements of the right stack to the left stack\n",
|
||||
"* Push the data to the left stack\n",
|
||||
"\n",
|
||||
|
@ -75,7 +79,7 @@
|
|||
"\n",
|
||||
"### Dequeue\n",
|
||||
"\n",
|
||||
"* If the right stack is empty and the the left stack is not empty\n",
|
||||
"* If the left stack is not empty\n",
|
||||
" * Shift the elements of the left stack to the right stack\n",
|
||||
"* Pop from the right stack and return the data\n",
|
||||
"\n",
|
||||
|
@ -130,13 +134,11 @@
|
|||
" destination.push(source.pop())\n",
|
||||
"\n",
|
||||
" def enqueue(self, data):\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",
|
||||
"\n",
|
||||
" def dequeue(self):\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()"
|
||||
]
|
||||
},
|
||||
|
@ -236,21 +238,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
Loading…
Reference in New Issue
Block a user