interactive-coding-challenges/stacks-queues/sort-stack.ipynb

153 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Sort a stack. You can use another stack as a buffer.\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
"* When sorted, should the largest element be at the top or bottom?\n",
" * Top\n",
"* Can you have duplicate values, saw two 5s?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* Empty stack\n",
"* One element stack\n",
"* Two or more element stack (general case)\n",
"* Already sorted stack"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"* Our buffer will hold elements in reverse sorted order, smallest at the top\n",
"* Store the current top element in a temp variable\n",
"* While stack is not empty\n",
" * While buffer is empty or buffer top is > than temp\n",
" * Move buffer top to stack\n",
" * Move temp to top of buffer\n",
"* Return buffer\n",
"\n",
"Complexity:\n",
"* Time: O(n^2)\n",
"* Space: O(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run stack.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class MyStack(Stack):\n",
" def sort(self):\n",
" buff = MyStack()\n",
" while not self.is_empty():\n",
" temp = self.pop()\n",
" while not buff.is_empty() and buff.peek() > temp:\n",
" self.push(buff.pop())\n",
" buff.push(temp)\n",
" return buff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from random import randint\n",
"\n",
"def test_sort(num_items, stack=None):\n",
" if stack is None:\n",
" stack = MyStack()\n",
" for _ in xrange(0, num_items):\n",
" stack.push(randint(0, num_items))\n",
" sorted_stack = stack.sort()\n",
" for _ in xrange(0, num_items):\n",
" print(sorted_stack.pop())\n",
" return sorted_stack\n",
"\n",
"print('Empty stack')\n",
"test_sort(0)\n",
"print('One element stack')\n",
"test_sort(1)\n",
"print('Two or more element stack (general case)')\n",
"sorted_stack = test_sort(5)"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}