interactive-coding-challenges/online_judges/sub_two/sub_two_solution.ipynb
2017-03-28 05:11:10 -04:00

211 lines
4.7 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: Sum of Two Integers (Subtraction Variant).\n",
"\n",
"See the [LeetCode](https://leetcode.com/problems/sum-of-two-integers/) problem page.\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",
"* Can we assume we're working with 32 bit ints?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No, check for None\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"<pre>\n",
"* None input -> TypeError\n",
"* 7, 5 -> 2\n",
"* -5, -7 -> 2\n",
"* -5, 7 -> -12\n",
"* 5, -7 -> 12\n",
"</pre>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"We'll look at the following example, subtracting a and b:\n",
"\n",
"<pre>\n",
"a 0110 = 6 \n",
"b 0101 = 5\n",
"</pre>\n",
"\n",
"First, subtract a and b, without worrying about the borrow (0-0=0, 0-1=1, 1-1=0):\n",
"\n",
"result = a ^ b = 0011\n",
"\n",
"Next, calculate the borrow (0-1=1). We'll need to left shift one to prepare for the next iteration when we move to the next most significant bit:\n",
"\n",
"~a = 1001\n",
" b = 0101\n",
"~a & b = 0001\n",
"\n",
"borrow = (~a&b) << 1 = 0010\n",
"\n",
"If the borrow is not zero, we'll need to subtract the borrow from the result. Recusively call the function, passing in result and borrow.\n",
"\n",
"Complexity:\n",
"* Time: O(b), where b is the number of bits\n",
"* Space: O(b), where b is the number of bits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def sub_two(self, a, b):\n",
" if a is None or b is None:\n",
" raise TypeError('a or b cannot be None')\n",
" result = a ^ b;\n",
" borrow = (~a&b) << 1\n",
" if borrow != 0:\n",
" return self.sub_two(result, borrow)\n",
" return result;"
]
},
{
"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_sub_two.py\n"
]
}
],
"source": [
"%%writefile test_sub_two.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestSubTwo(object):\n",
"\n",
" def test_sub_two(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.sub_two, None)\n",
" assert_equal(solution.sub_two(7, 5), 2)\n",
" assert_equal(solution.sub_two(-5, -7), 2)\n",
" assert_equal(solution.sub_two(-5, 7), -12)\n",
" assert_equal(solution.sub_two(5, -7), 12)\n",
" print('Success: test_sub_two')\n",
"\n",
"\n",
"def main():\n",
" test = TestSubTwo()\n",
" test.test_sub_two()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_sub_two\n"
]
}
],
"source": [
"%run -i test_sub_two.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
}