mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook solving the following: Implement n stacks using a single array.
This commit is contained in:
parent
54246a328a
commit
1d38885098
|
@ -32,6 +32,7 @@ Continually updated IPython Notebooks containing coding problems and solutions (
|
|||
|
||||
* [Implement a stack with push, pop, and peek methods using a linked list](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/stack.ipynb)
|
||||
* [Implement a queue with enqueue and dequeue methods using a linked list](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/queue.ipynb)
|
||||
* [Implement n stacks using a single array](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/n-stacks.ipynb)
|
||||
|
||||
## License
|
||||
|
||||
|
|
184
stacks-queues/n-stacks.ipynb
Normal file
184
stacks-queues/n-stacks.ipynb
Normal file
|
@ -0,0 +1,184 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Implement n stacks using a single array.\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"\n",
|
||||
"* Are the stacks and array a fixed size?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* Test the following on the three stacks:\n",
|
||||
" * Push to full stack\n",
|
||||
" * Push to non-full stack\n",
|
||||
" * Pop on empty stack\n",
|
||||
" * Pop on non-empty stack"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"* Initialize array\n",
|
||||
"* Initialize stack pointers to -1\n",
|
||||
"\n",
|
||||
"### Push\n",
|
||||
"\n",
|
||||
"* If stack is full, throw exception\n",
|
||||
"* Else\n",
|
||||
" * Increment stack pointer\n",
|
||||
" * Get the absolute array index\n",
|
||||
" * Insert the value to this index\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)\n",
|
||||
"\n",
|
||||
"### Pop\n",
|
||||
"\n",
|
||||
"* If stack is empty, throw exception\n",
|
||||
"* Else\n",
|
||||
" * Store the value contained in the absolute array index\n",
|
||||
" * Set the value in the absolute array index to None\n",
|
||||
" * Decrement stack pointer\n",
|
||||
" * return value\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Absolute Index\n",
|
||||
"\n",
|
||||
"* return stack size * stack index + stack pointer\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Stacks(object):\n",
|
||||
" def __init__(self, num_stacks, stack_size):\n",
|
||||
" self.num_stacks = num_stacks\n",
|
||||
" self.stack_size = stack_size\n",
|
||||
" self.stack_pointers = [-1] * num_stacks\n",
|
||||
" self.stack_array = [None] * num_stacks * stack_size\n",
|
||||
"\n",
|
||||
" def abs_index(self, stack_index):\n",
|
||||
" return stack_index * self.stack_size + self.stack_pointers[stack_index]\n",
|
||||
"\n",
|
||||
" def push(self, stack_index, data):\n",
|
||||
" if self.stack_pointers[stack_index] == self.stack_size - 1:\n",
|
||||
" raise Exception('Stack is full')\n",
|
||||
" else:\n",
|
||||
" self.stack_pointers[stack_index] += 1\n",
|
||||
" array_index = self.abs_index(stack_index)\n",
|
||||
" self.stack_array[array_index] = data\n",
|
||||
"\n",
|
||||
" def pop(self, stack_index):\n",
|
||||
" if self.stack_pointers[stack_index] == -1:\n",
|
||||
" raise Exception('Stack is empty')\n",
|
||||
" else:\n",
|
||||
" array_index = self.abs_index(stack_index) \n",
|
||||
" data = self.stack_array[array_index]\n",
|
||||
" self.stack_array[array_index] = None\n",
|
||||
" self.stack_pointers[stack_index] -= 1\n",
|
||||
" return data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_stacks = 3\n",
|
||||
"stack_size = 100\n",
|
||||
"print('Pop on empty stack')\n",
|
||||
"stacks = Stacks(num_stacks, stack_size)\n",
|
||||
"try:\n",
|
||||
" stacks.pop(0)\n",
|
||||
"except Exception as e:\n",
|
||||
" print(e)\n",
|
||||
"print('Push to non-full stack')\n",
|
||||
"stacks.push(0, 1)\n",
|
||||
"stacks.push(0, 2)\n",
|
||||
"stacks.push(1, 3)\n",
|
||||
"stacks.push(2, 4)\n",
|
||||
"print('Pop on non-empty stack')\n",
|
||||
"print(stacks.pop(0))\n",
|
||||
"print(stacks.pop(0))\n",
|
||||
"print(stacks.pop(1))\n",
|
||||
"print(stacks.pop(2))\n",
|
||||
"print('Push to full stack')\n",
|
||||
"for i in xrange(0, stack_size):\n",
|
||||
" stacks.push(2, i)\n",
|
||||
"try:\n",
|
||||
" stacks.push(2, stack_size)\n",
|
||||
"except Exception as e:\n",
|
||||
" print(e)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
Loading…
Reference in New Issue
Block a user