mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Add power two challenge
This commit is contained in:
parent
fed90d0fd8
commit
3395619d6e
0
math_probability/power_two/__init__.py
Normal file
0
math_probability/power_two/__init__.py
Normal file
171
math_probability/power_two/power_two_challenge.ipynb
Normal file
171
math_probability/power_two/power_two_challenge.ipynb
Normal file
|
@ -0,0 +1,171 @@
|
||||||
|
{
|
||||||
|
"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: 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)\n",
|
||||||
|
"* [Solution Notebook](#Solution-Notebook)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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",
|
||||||
|
"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 is_power_of_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_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": "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
|
||||||
|
}
|
206
math_probability/power_two/power_two_solution.ipynb
Normal file
206
math_probability/power_two/power_two_solution.ipynb
Normal 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: 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
|
||||||
|
}
|
23
math_probability/power_two/test_is_power_of_two.py
Normal file
23
math_probability/power_two/test_is_power_of_two.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
from nose.tools import assert_equal, assert_raises
|
||||||
|
|
||||||
|
|
||||||
|
class TestSolution(object):
|
||||||
|
|
||||||
|
def test_is_power_of_two(self):
|
||||||
|
solution = Solution()
|
||||||
|
assert_raises(TypeError, solution.is_power_of_two, None)
|
||||||
|
assert_equal(solution.is_power_of_two(0), False)
|
||||||
|
assert_equal(solution.is_power_of_two(1), True)
|
||||||
|
assert_equal(solution.is_power_of_two(2), True)
|
||||||
|
assert_equal(solution.is_power_of_two(15), False)
|
||||||
|
assert_equal(solution.is_power_of_two(16), True)
|
||||||
|
print('Success: test_is_power_of_two')
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test = TestSolution()
|
||||||
|
test.test_is_power_of_two()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user