Polish n stacks solution

This commit is contained in:
Donne Martin 2016-02-21 16:20:29 -05:00
parent 9c441e9f18
commit 76da9ec98d

View File

@ -119,20 +119,18 @@
" def push(self, stack_index, data):\n", " def push(self, stack_index, data):\n",
" if self.stack_pointers[stack_index] == self.stack_size - 1:\n", " if self.stack_pointers[stack_index] == self.stack_size - 1:\n",
" raise Exception('Stack is full')\n", " raise Exception('Stack is full')\n",
" else:\n", " self.stack_pointers[stack_index] += 1\n",
" self.stack_pointers[stack_index] += 1\n", " array_index = self.abs_index(stack_index)\n",
" array_index = self.abs_index(stack_index)\n", " self.stack_array[array_index] = data\n",
" self.stack_array[array_index] = data\n",
"\n", "\n",
" def pop(self, stack_index):\n", " def pop(self, stack_index):\n",
" if self.stack_pointers[stack_index] == -1:\n", " if self.stack_pointers[stack_index] == -1:\n",
" raise Exception('Stack is empty')\n", " raise Exception('Stack is empty')\n",
" else:\n", " array_index = self.abs_index(stack_index)\n",
" array_index = self.abs_index(stack_index)\n", " data = self.stack_array[array_index]\n",
" data = self.stack_array[array_index]\n", " self.stack_array[array_index] = None\n",
" self.stack_array[array_index] = None\n", " self.stack_pointers[stack_index] -= 1\n",
" self.stack_pointers[stack_index] -= 1\n", " return data"
" return data"
] ]
}, },
{ {
@ -237,21 +235,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.4.3"
} }
}, },
"nbformat": 4, "nbformat": 4,