interactive-coding-challenges/math_probability/power_two/power_two_solution.ipynb

207 lines
4.4 KiB
Python
Raw Normal View History

2017-03-29 16:41:58 +08:00
{
"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: Determine if a number is a power of two.\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 the input number an int?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Is the output a boolean?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> TypeError\n",
"* 0 -> False\n",
"* 1 -> True\n",
"* 2 -> True\n",
"* 15 -> False\n",
"* 16 -> True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"We can use bit manipulation to determine if a number is a power of two. \n",
"\n",
"For a number to be a power of two, there must only be one bit that is a `1`. \n",
"\n",
"We can use the following bit manipulation trick to determine this:\n",
"\n",
"`n & (n - 1)`\n",
"\n",
"Here's an example why:\n",
"\n",
"<pre>\n",
"0000 1000 = n\n",
"0000 0001 = 1\n",
"0000 0111 = n-1\n",
"\n",
"0000 1000 = n\n",
"0000 0111 = n-1\n",
"0000 0000 = n & n-1, result = 0\n",
"</pre>\n",
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def is_power_of_two(self, n):\n",
" if n is None:\n",
" raise TypeError('n cannot be None')\n",
" if n <= 0:\n",
" return False\n",
" return (n & (n - 1)) == 0"
]
},
{
"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_is_power_of_two.py\n"
]
}
],
"source": [
"%%writefile test_is_power_of_two.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestSolution(object):\n",
"\n",
" def test_is_power_of_two(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.is_power_of_two, None)\n",
" assert_equal(solution.is_power_of_two(0), False)\n",
" assert_equal(solution.is_power_of_two(1), True)\n",
" assert_equal(solution.is_power_of_two(2), True)\n",
" assert_equal(solution.is_power_of_two(15), False)\n",
" assert_equal(solution.is_power_of_two(16), True)\n",
" print('Success: test_is_power_of_two')\n",
"\n",
"\n",
"def main():\n",
" test = TestSolution()\n",
" test.test_is_power_of_two()\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_is_power_of_two\n"
]
}
],
"source": [
"%run -i test_is_power_of_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
}