{ "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": {}, "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": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test_merge_into.py\n" ] } ], "source": [ "%%writefile test_merge_into.py\n", "import unittest\n", "\n", "\n", "class TestArray(unittest.TestCase):\n", "\n", " def test_merge_into(self):\n", " array = Array()\n", " self.assertRaises(TypeError, array.merge_into, None, None, None, None)\n", " self.assertRaises(ValueError, array.merge_into, [1], [2], -1, -1)\n", " a = [1, 2, 3]\n", " self.assertEqual(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n", " a = [1, 2, 3]\n", " self.assertEqual(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", " self.assertEqual(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": {}, "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.7.2" } }, "nbformat": 4, "nbformat_minor": 1 }