Add sub two challenge

This commit is contained in:
Donne Martin 2017-03-29 04:41:24 -04:00
parent 93e1558d43
commit fed90d0fd8
4 changed files with 395 additions and 0 deletions

View File

View File

@ -0,0 +1,167 @@
{
"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: Find the difference of two integers without using the + or - sign.\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",
"* 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",
"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 sub_two(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_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": "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
}

View File

@ -0,0 +1,206 @@
{
"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: Find the difference of two integers without using the + or - sign.\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 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
}

View File

@ -0,0 +1,22 @@
from nose.tools import assert_equal, assert_raises
class TestSubTwo(object):
def test_sub_two(self):
solution = Solution()
assert_raises(TypeError, solution.sub_two, None)
assert_equal(solution.sub_two(7, 5), 2)
assert_equal(solution.sub_two(-5, -7), 2)
assert_equal(solution.sub_two(-5, 7), -12)
assert_equal(solution.sub_two(5, -7), 12)
print('Success: test_sub_two')
def main():
test = TestSubTwo()
test.test_sub_two()
if __name__ == '__main__':
main()