mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-05 00:48:03 +00:00
Polish queue from stacks challenge and solution (#74)
Update constraints, algorithm discussion, and code.
This commit is contained in:
@@ -34,9 +34,13 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\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",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -179,21 +183,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,
|
||||||
|
|||||||
@@ -33,9 +33,13 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\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",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -65,7 +69,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Enqueue\n",
|
"### Enqueue\n",
|
||||||
"\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",
|
" * Shift the elements of the right stack to the left stack\n",
|
||||||
"* Push the data to the left stack\n",
|
"* Push the data to the left stack\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -75,7 +79,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Dequeue\n",
|
"### Dequeue\n",
|
||||||
"\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",
|
" * Shift the elements of the left stack to the right stack\n",
|
||||||
"* Pop from the right stack and return the data\n",
|
"* Pop from the right stack and return the data\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -130,13 +134,11 @@
|
|||||||
" destination.push(source.pop())\n",
|
" destination.push(source.pop())\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def enqueue(self, data):\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",
|
" self.left_stack.push(data)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def dequeue(self):\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()"
|
" return self.right_stack.pop()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -236,21 +238,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,
|
||||||
|
|||||||
Reference in New Issue
Block a user