mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
272 lines
6.0 KiB
Python
272 lines
6.0 KiB
Python
{
|
|
"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",
|
|
"<pre>\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",
|
|
"</pre>\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
|
|
}
|