diff --git a/arrays_strings/two_sum/__init__.py b/arrays_strings/two_sum/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_strings/two_sum/test_two_sum.py b/arrays_strings/two_sum/test_two_sum.py new file mode 100644 index 0000000..9f6fff3 --- /dev/null +++ b/arrays_strings/two_sum/test_two_sum.py @@ -0,0 +1,23 @@ +from nose.tools import assert_equal, assert_raises + + +class TestTwoSum(object): + + def test_two_sum(self): + solution = Solution() + assert_raises(TypeError, solution.two_sum, None, None) + assert_raises(ValueError, solution.two_sum, [], 0) + target = 7 + nums = [1, 3, 2, -7, 5] + expected = [2, 4] + assert_equal(solution.two_sum(nums, target), expected) + print('Success: test_two_sum') + + +def main(): + test = TestTwoSum() + test.test_two_sum() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/arrays_strings/two_sum/two_sum_challenge.ipynb b/arrays_strings/two_sum/two_sum_challenge.ipynb new file mode 100644 index 0000000..733b9ec --- /dev/null +++ b/arrays_strings/two_sum/two_sum_challenge.ipynb @@ -0,0 +1,170 @@ +{ + "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 an array, find the two indices that sum to a specific value.\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", + "* Is there exactly one solution?\n", + " * Yes\n", + "* Is there always a solution?\n", + " * Yes\n", + "* Is the array an array of ints?\n", + " * Yes\n", + "* Is the array sorted?\n", + " No\n", + "* Are negative values possible?\n", + " * Yes\n", + "* Can we assume the inputs are valid?\n", + " * No\n", + "* Can we assume this fits memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* None input -> TypeError\n", + "* [] -> ValueError\n", + "* [1, 3, 2, -7, 5], 7 -> [2, 4]" + ] + }, + { + "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 Solution(object):\n", + "\n", + " def two_sum(self, val):\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_two_sum.py\n", + "from nose.tools import assert_equal, assert_raises\n", + "\n", + "\n", + "class TestTwoSum(object):\n", + "\n", + " def test_two_sum(self):\n", + " solution = Solution()\n", + " assert_raises(TypeError, solution.two_sum, None)\n", + " assert_equal(solution.two_sum(0), 0)\n", + " print('Success: test_two_sum')\n", + "\n", + "\n", + "def main():\n", + " test = TestTwoSum()\n", + " test.test_two_sum()\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/arrays_strings/two_sum/two_sum_solution.ipynb b/arrays_strings/two_sum/two_sum_solution.ipynb new file mode 100644 index 0000000..ad44d07 --- /dev/null +++ b/arrays_strings/two_sum/two_sum_solution.ipynb @@ -0,0 +1,271 @@ +{ + "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 an array, find the two indices that sum to a specific value.\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", + "* Is there exactly one solution?\n", + " * Yes\n", + "* Is there always a solution?\n", + " * Yes\n", + "* Is the array an array of ints?\n", + " * Yes\n", + "* Is the array sorted?\n", + " No\n", + "* Are negative values possible?\n", + " * Yes\n", + "* Can we assume the inputs are valid?\n", + " * No\n", + "* Can we assume this fits memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* None input -> TypeError\n", + "* [] -> ValueError\n", + "* [1, 3, 2, -7, 5], 7 -> [2, 4]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "### Brute force\n", + "\n", + "* For i in range(len(input)):\n", + " * For j in range(i+1, len(input)):\n", + " * if i + j == target return True\n", + "* return False\n", + "\n", + "Complexity:\n", + "* Time: O(n^2)\n", + "* Space: O(1)\n", + "\n", + "### Optimized\n", + "\n", + "
\n",
+    "* Loop through each num in nums\n",
+    "    * Calculate the cache_target = target - num\n",
+    "\n",
+    "target = 7\n",
+    "index  =  0  1  2   3  4\n",
+    "nums   = [1, 3, 2, -7, 5]\n",
+    "          ^\n",
+    "cache_target = 7 - 1 = 6\n",
+    "cache\n",
+    "6 -> 0\n",
+    "\n",
+    "1 not in cache\n",
+    "\n",
+    "index  =  0  1  2   3  4\n",
+    "nums   = [1, 3, 2, -7, 5]\n",
+    "             ^\n",
+    "cache_target = 7 - 3 = 4\n",
+    "cache\n",
+    "6 -> 0\n",
+    "4 -> 1\n",
+    "\n",
+    "3 not in cache\n",
+    "\n",
+    "index  =  0  1  2   3  4\n",
+    "nums   = [1, 3, 2, -7, 5]\n",
+    "                ^\n",
+    "cache_target = 7 - 2 = 5\n",
+    "cache\n",
+    "6 -> 0\n",
+    "4 -> 1\n",
+    "5 -> 2\n",
+    "\n",
+    "2 not in cache\n",
+    "\n",
+    "index  =  0  1  2   3  4\n",
+    "nums   = [1, 3, 2, -7, 5]\n",
+    "                    ^\n",
+    "cache_target = 7 + 7 = 14\n",
+    "cache\n",
+    "6  -> 0\n",
+    "4  -> 1\n",
+    "5  -> 2\n",
+    "14 -> 3\n",
+    "\n",
+    "-7 not in cache\n",
+    "\n",
+    "index  =  0  1  2   3  4\n",
+    "nums   = [1, 3, 2, -7, 5]\n",
+    "                       ^\n",
+    "cache_target = 7 - 5 = 2\n",
+    "cache\n",
+    "6  -> 0\n",
+    "4  -> 1\n",
+    "5  -> 2\n",
+    "14 -> 3\n",
+    "\n",
+    "5 in cache, success, output matching indices: cache[num] and current iteration index\n",
+    "\n",
+    "output = [2, 4]\n",
+    "
\n", + "\n", + "Complexity:\n", + "* Time: O(n)\n", + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "class Solution(object):\n", + "\n", + " def two_sum(self, nums, target):\n", + " if nums is None or target is None:\n", + " raise TypeError('nums or target cannot be None')\n", + " if not nums:\n", + " raise ValueError('nums cannot be empty')\n", + " cache = {}\n", + " for index, num in enumerate(nums):\n", + " cache_target = target - num\n", + " if num in cache:\n", + " return [cache[num], index]\n", + " else:\n", + " cache[cache_target] = index\n", + " return None" + ] + }, + { + "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_two_sum.py\n" + ] + } + ], + "source": [ + "%%writefile test_two_sum.py\n", + "from nose.tools import assert_equal, assert_raises\n", + "\n", + "\n", + "class TestTwoSum(object):\n", + "\n", + " def test_two_sum(self):\n", + " solution = Solution()\n", + " assert_raises(TypeError, solution.two_sum, None, None)\n", + " assert_raises(ValueError, solution.two_sum, [], 0)\n", + " target = 7\n", + " nums = [1, 3, 2, -7, 5]\n", + " expected = [2, 4]\n", + " assert_equal(solution.two_sum(nums, target), expected)\n", + " print('Success: test_two_sum')\n", + "\n", + "\n", + "def main():\n", + " test = TestTwoSum()\n", + " test.test_two_sum()\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_two_sum\n" + ] + } + ], + "source": [ + "%run -i test_two_sum.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 +}