diff --git a/sorting_searching/merge_into/__init__.py b/sorting_searching/merge_into/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sorting_searching/merge_into/merge_into_challenge.ipynb b/sorting_searching/merge_into/merge_into_challenge.ipynb new file mode 100644 index 0000000..63fdd95 --- /dev/null +++ b/sorting_searching/merge_into/merge_into_challenge.ipynb @@ -0,0 +1,178 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Given sorted arrays A, B, merge B into A in sorted order.\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", + "* Does A have enough space for B?\n", + " * Yes\n", + "* Can the inputs have duplicate array items?\n", + " * Yes\n", + "* Can we assume the inputs are valid?\n", + " * No\n", + "* Does the inputs also include the actual size of A and B?\n", + " * Yes\n", + "* Can we assume this fits memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* A or B is None -> Exception\n", + "* index of last A or B < 0 -> Exception\n", + "* A or B is empty\n", + "* General case\n", + " * A = [1, 3, 5, 7, 9, None, None, None]\n", + " * B = [4, 5, 6]\n", + " * A = [1, 3, 4, 5, 5, 6, 7, 9]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](). 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": false + }, + "outputs": [], + "source": [ + "class Array(object):\n", + "\n", + " def merge_into(self, source, dest, source_end_index, dest_end_index):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**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_merge_into.py\n", + "from nose.tools import assert_equal, assert_raises\n", + "\n", + "\n", + "class TestArray(object):\n", + "\n", + " def test_merge_into(self):\n", + " array = Array()\n", + " assert_raises(TypeError, array.merge_into, None, None, None, None)\n", + " assert_raises(ValueError, array.merge_into, [1], [2], -1, -1)\n", + " a = [1, 2, 3]\n", + " assert_equal(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n", + " a = [1, 2, 3]\n", + " assert_equal(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n", + " a = [1, 3, 5, 7, 9, None, None, None]\n", + " b = [4, 5, 6]\n", + " expected = [1, 3, 4, 5, 5, 6, 7, 9]\n", + " assert_equal(array.merge_into(a, b, 5, len(b)), expected)\n", + " print('Success: test_merge_into')\n", + "\n", + "\n", + "def main():\n", + " test = TestArray()\n", + " test.test_merge_into()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook]() for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/sorting_searching/merge_into/merge_into_solution.ipynb b/sorting_searching/merge_into/merge_into_solution.ipynb new file mode 100644 index 0000000..2df970e --- /dev/null +++ b/sorting_searching/merge_into/merge_into_solution.ipynb @@ -0,0 +1,276 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Given sorted arrays A, B, merge B into A in sorted order.\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* Does A have enough space for B?\n", + " * Yes\n", + "* Can the inputs have duplicate array items?\n", + " * Yes\n", + "* Can we assume the inputs are valid?\n", + " * No\n", + "* Does the inputs also include the actual size of A and B?\n", + " * Yes\n", + "* Can we assume this fits memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* A or B is None -> Exception\n", + "* index of last A or B < 0 -> Exception\n", + "* A or B is empty\n", + "* General case\n", + " * A = [1, 3, 5, 7, 9, None, None, None]\n", + " * B = [4, 5, 6]\n", + " * A = [1, 3, 4, 5, 5, 6, 7, 9]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "
\n", + " i k\n", + "A = [1, 3, 5, 7, 9, None, None, None]\n", + " j\n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " i k\n", + "A = [1, 3, 5, 7, 9, None, None, 9]\n", + " j\n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " i k \n", + "A = [1, 3, 5, 7, 9, None, 7, 9]\n", + " j\n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " i k \n", + "A = [1, 3, 5, 7, 9, 6, 7, 9]\n", + " j\n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " i k \n", + "A = [1, 3, 5, 7, 5, 6, 7, 9]\n", + " j \n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " i k \n", + "A = [1, 3, 5, 5, 5, 6, 7, 9]\n", + " j \n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " i k \n", + "A = [1, 3, 4, 5, 5, 6, 7, 9]\n", + " j \n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A[k] = max(A[i], B[j])\n", + " ik \n", + "A = [1, 3, 4, 5, 5, 6, 7, 9]\n", + " \n", + "B = [4, 5, 6]\n", + "\n", + "---\n", + "\n", + "A = [1, 3, 4, 5, 5, 6, 7, 9]\n", + "\n", + "\n", + "\n", + "Complexity:\n", + "* Time: O(m + n)\n", + "* Space: O(1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "class Array(object):\n", + "\n", + " def merge_into(self, source, dest, source_end_index, dest_end_index):\n", + " if source is None or dest is None:\n", + " raise TypeError('source or dest cannot be None')\n", + " if source_end_index < 0 or dest_end_index < 0:\n", + " raise ValueError('end indices must be >= 0')\n", + " if not source:\n", + " return dest\n", + " if not dest:\n", + " return source\n", + " source_index = source_end_index - 1\n", + " dest_index = dest_end_index - 1\n", + " insert_index = source_end_index + dest_end_index - 1\n", + " while dest_index >= 0:\n", + " if source[source_index] > dest[dest_index]:\n", + " source[insert_index] = source[source_index]\n", + " source_index -= 1\n", + " else:\n", + " source[insert_index] = dest[dest_index]\n", + " dest_index -= 1\n", + " insert_index -= 1\n", + " return source" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_merge_into.py\n" + ] + } + ], + "source": [ + "%%writefile test_merge_into.py\n", + "from nose.tools import assert_equal, assert_raises\n", + "\n", + "\n", + "class TestArray(object):\n", + "\n", + " def test_merge_into(self):\n", + " array = Array()\n", + " assert_raises(TypeError, array.merge_into, None, None, None, None)\n", + " assert_raises(ValueError, array.merge_into, [1], [2], -1, -1)\n", + " a = [1, 2, 3]\n", + " assert_equal(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n", + " a = [1, 2, 3]\n", + " assert_equal(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n", + " a = [1, 3, 5, 7, 9, None, None, None]\n", + " b = [4, 5, 6]\n", + " expected = [1, 3, 4, 5, 5, 6, 7, 9]\n", + " assert_equal(array.merge_into(a, b, 5, len(b)), expected)\n", + " print('Success: test_merge_into')\n", + "\n", + "\n", + "def main():\n", + " test = TestArray()\n", + " test.test_merge_into()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_merge_into\n" + ] + } + ], + "source": [ + "%run -i test_merge_into.py" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/sorting_searching/merge_into/test_merge_into.py b/sorting_searching/merge_into/test_merge_into.py new file mode 100644 index 0000000..b0e7d84 --- /dev/null +++ b/sorting_searching/merge_into/test_merge_into.py @@ -0,0 +1,27 @@ +from nose.tools import assert_equal, assert_raises + + +class TestArray(object): + + def test_merge_into(self): + array = Array() + assert_raises(TypeError, array.merge_into, None, None, None, None) + assert_raises(ValueError, array.merge_into, [1], [2], -1, -1) + a = [1, 2, 3] + assert_equal(array.merge_into(a, [], len(a), 0), [1, 2, 3]) + a = [1, 2, 3] + assert_equal(array.merge_into(a, [], len(a), 0), [1, 2, 3]) + a = [1, 3, 5, 7, 9, None, None, None] + b = [4, 5, 6] + expected = [1, 3, 4, 5, 5, 6, 7, 9] + assert_equal(array.merge_into(a, b, 5, len(b)), expected) + print('Success: test_merge_into') + + +def main(): + test = TestArray() + test.test_merge_into() + + +if __name__ == '__main__': + main() \ No newline at end of file