diff --git a/README.md b/README.md index 538da47..c3b4ebe 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Continually updated IPython Notebooks containing coding problems and solutions ( * [Implement a set of stacks class that wraps a list of stacks, each bound by a capacity](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/set-of-stacks.ipynb) * [Implement the Towers of Hanoi with 3 towers and N disks](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/hanoi.ipynb) * [Implement a queue using two stacks](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/queue-from-stacks.ipynb) +* [Sort a stack using another stack as a buffer](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/sort-stack.ipynb) ## Hacker Rank diff --git a/stacks-queues/sort-stack.ipynb b/stacks-queues/sort-stack.ipynb new file mode 100644 index 0000000..7f7cc02 --- /dev/null +++ b/stacks-queues/sort-stack.ipynb @@ -0,0 +1,145 @@ +{ + "cells": [ + { + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}