From adf539cdfe6c4b9e08b6cde19bfbf9dd0296a96a Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Thu, 2 Jul 2015 23:04:15 -0400 Subject: [PATCH] Added n-stacks challenge. --- .../n_stacks/n_stacks_challenge.ipynb | 198 ++++++++++++++++++ .../n_stacks/n_stacks_solution.ipynb | 64 ++++-- stacks_queues/n_stacks/test_n_stacks.py | 46 ++++ 3 files changed, 292 insertions(+), 16 deletions(-) create mode 100644 stacks_queues/n_stacks/n_stacks_challenge.ipynb create mode 100644 stacks_queues/n_stacks/test_n_stacks.py diff --git a/stacks_queues/n_stacks/n_stacks_challenge.ipynb b/stacks_queues/n_stacks/n_stacks_challenge.ipynb new file mode 100644 index 0000000..227514a --- /dev/null +++ b/stacks_queues/n_stacks/n_stacks_challenge.ipynb @@ -0,0 +1,198 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Implement n stacks using a single array.\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\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 -> Exception\n", + " * Push to non-full stack\n", + " * Pop on empty stack -> Exception\n", + " * Pop on non-empty stack" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/stacks_queues/n_stacks/n_stacks_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Stacks(object):\n", + " \n", + " def __init__(self, num_stacks, stack_size):\n", + " # TODO: Implement me\n", + " pass\n", + "\n", + " def abs_index(self, stack_index):\n", + " # TODO: Implement me\n", + " pass\n", + "\n", + " def push(self, stack_index, data):\n", + " # TODO: Implement me\n", + " pass\n", + "\n", + " def pop(self, stack_index):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\n", + "\n", + "\n", + "\n", + "**The following unit test is expected to fail until you solve the challenge.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# %load test_n_stacks.py\n", + "from nose.tools import assert_equal\n", + "from nose.tools import raises\n", + "\n", + "\n", + "class TestStacks(object):\n", + " \n", + " @raises(Exception)\n", + " def test_pop_on_empty(self, num_stacks, stack_size):\n", + " print('Test: Pop on empty stack')\n", + " stacks = Stacks(num_stacks, stack_size)\n", + " stacks.pop(0)\n", + "\n", + " @raises(Exception)\n", + " def test_push_on_full(self, num_stacks, stack_size):\n", + " print('Test: Push to full stack')\n", + " stacks = Stacks(num_stacks, stack_size)\n", + " for i in xrange(0, stack_size):\n", + " stacks.push(2, i)\n", + " stacks.push(2, stack_size)\n", + "\n", + " def test_stacks(self, num_stacks, stack_size):\n", + " print('Test: Push to non-full stack')\n", + " stacks = Stacks(num_stacks, stack_size)\n", + " stacks.push(0, 1)\n", + " stacks.push(0, 2)\n", + " stacks.push(1, 3)\n", + " stacks.push(2, 4)\n", + "\n", + " print('Test: Pop on non-empty stack')\n", + " assert_equal(stacks.pop(0), 2)\n", + " assert_equal(stacks.pop(0), 1)\n", + " assert_equal(stacks.pop(1), 3)\n", + " assert_equal(stacks.pop(2), 4)\n", + "\n", + " print('Success: test_stacks\\n')\n", + "\n", + "def main():\n", + " num_stacks = 3\n", + " stack_size = 100 \n", + " test = TestStacks()\n", + " test.test_pop_on_empty(num_stacks, stack_size)\n", + " test.test_push_on_full(num_stacks, stack_size)\n", + " test.test_stacks(num_stacks, stack_size)\n", + " \n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/stacks_queues/n_stacks/n_stacks_solution.ipynb) for a discussion on algorithms and code solutions." + ] + } + ], + "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 +} diff --git a/stacks_queues/n_stacks/n_stacks_solution.ipynb b/stacks_queues/n_stacks/n_stacks_solution.ipynb index b54e618..34ce773 100644 --- a/stacks_queues/n_stacks/n_stacks_solution.ipynb +++ b/stacks_queues/n_stacks/n_stacks_solution.ipynb @@ -4,7 +4,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes)." + "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" ] }, { @@ -26,7 +33,7 @@ "source": [ "## Constraints\n", "\n", - "*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", + "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", "\n", "* Are the stacks and array a fixed size?\n", " * Yes" @@ -39,9 +46,9 @@ "## Test Cases\n", "\n", "* Test the following on the three stacks:\n", - " * Push to full stack\n", + " * Push to full stack -> Exception\n", " * Push to non-full stack\n", - " * Pop on empty stack\n", + " * Pop on empty stack -> Exception\n", " * Pop on non-empty stack" ] }, @@ -101,6 +108,7 @@ "outputs": [], "source": [ "class Stacks(object):\n", + " \n", " def __init__(self, num_stacks, stack_size):\n", " self.num_stacks = num_stacks\n", " self.stack_size = stack_size\n", @@ -134,8 +142,7 @@ "metadata": {}, "source": [ "## Unit Test\n", - "\n", - "*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*" + "\n" ] }, { @@ -149,20 +156,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "Test: Pop on empty stack\n", - "Test: Push to full stack\n", - "Test: Push to non-full stack\n", - "Test: Pop on non-empty stack\n", - "Success: test_stacks\n", - "\n" + "Overwriting test_n_stacks.py\n" ] } ], "source": [ + "%%writefile test_n_stacks.py\n", "from nose.tools import assert_equal\n", "from nose.tools import raises\n", "\n", - "class Test(object):\n", + "\n", + "class TestStacks(object):\n", + " \n", " @raises(Exception)\n", " def test_pop_on_empty(self, num_stacks, stack_size):\n", " print('Test: Pop on empty stack')\n", @@ -193,13 +198,40 @@ "\n", " print('Success: test_stacks\\n')\n", "\n", - "if __name__ == '__main__':\n", + "def main():\n", " num_stacks = 3\n", " stack_size = 100 \n", - " test = Test()\n", + " test = TestStacks()\n", " test.test_pop_on_empty(num_stacks, stack_size)\n", " test.test_push_on_full(num_stacks, stack_size)\n", - " test.test_stacks(num_stacks, stack_size)" + " test.test_stacks(num_stacks, stack_size)\n", + " \n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test: Pop on empty stack\n", + "Test: Push to full stack\n", + "Test: Push to non-full stack\n", + "Test: Pop on non-empty stack\n", + "Success: test_stacks\n", + "\n" + ] + } + ], + "source": [ + "run -i test_n_stacks.py" ] } ], diff --git a/stacks_queues/n_stacks/test_n_stacks.py b/stacks_queues/n_stacks/test_n_stacks.py new file mode 100644 index 0000000..bee6f48 --- /dev/null +++ b/stacks_queues/n_stacks/test_n_stacks.py @@ -0,0 +1,46 @@ +from nose.tools import assert_equal +from nose.tools import raises + + +class TestStacks(object): + + @raises(Exception) + def test_pop_on_empty(self, num_stacks, stack_size): + print('Test: Pop on empty stack') + stacks = Stacks(num_stacks, stack_size) + stacks.pop(0) + + @raises(Exception) + def test_push_on_full(self, num_stacks, stack_size): + print('Test: Push to full stack') + stacks = Stacks(num_stacks, stack_size) + for i in xrange(0, stack_size): + stacks.push(2, i) + stacks.push(2, stack_size) + + def test_stacks(self, num_stacks, stack_size): + print('Test: Push to non-full stack') + stacks = Stacks(num_stacks, stack_size) + stacks.push(0, 1) + stacks.push(0, 2) + stacks.push(1, 3) + stacks.push(2, 4) + + print('Test: Pop on non-empty stack') + assert_equal(stacks.pop(0), 2) + assert_equal(stacks.pop(0), 1) + assert_equal(stacks.pop(1), 3) + assert_equal(stacks.pop(2), 4) + + print('Success: test_stacks\n') + +def main(): + num_stacks = 3 + stack_size = 100 + test = TestStacks() + test.test_pop_on_empty(num_stacks, stack_size) + test.test_push_on_full(num_stacks, stack_size) + test.test_stacks(num_stacks, stack_size) + +if __name__ == '__main__': + main() \ No newline at end of file