Add check prime challenge (#226)

This commit is contained in:
Donne Martin 2018-01-19 19:14:17 -08:00 committed by GitHub
parent da5491b8a3
commit b4a64f78dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 395 additions and 0 deletions

View File

View File

@ -0,0 +1,166 @@
{
"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: Check if a number is prime.\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 it correct that 1 is not considered a prime number?\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 -> Exception\n",
"* Not an int -> Exception\n",
"* Less than 2 -> False\n",
"* General case"
]
},
{
"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 Math(object):\n",
"\n",
" def check_prime(self, num):\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_check_prime.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestMath(object):\n",
"\n",
" def test_check_prime(self):\n",
" math = Math()\n",
" assert_raises(TypeError, math.check_prime, None)\n",
" assert_raises(TypeError, math.check_prime, 98.6)\n",
" assert_equal(math.check_prime(0), False)\n",
" assert_equal(math.check_prime(1), False)\n",
" assert_equal(math.check_prime(97), True)\n",
" print('Success: test_check_prime')\n",
"\n",
"\n",
"def main():\n",
" test = TestMath()\n",
" test.test_check_prime()\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,207 @@
{
"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: Check if a number is prime.\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 it correct that 1 is not considered a prime number?\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 -> Exception\n",
"* Not an int -> Exception\n",
"* Less than 2 -> False\n",
"* General case"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"For a number to be prime, it must be 2 or greater and cannot be divisible by another number other than itself (and 1).\n",
"\n",
"We'll check by dividing all numbers from 2 to the input number to determine if the number is prime.\n",
"\n",
"As an optimization, we can divide from 2 to the square root of the input number. For each value that divides the input number evenly, there is a complement b where a * b = n. If a > sqrt(n) then b < sqrt(n) because sqrt(n^2) = n.\n",
"\n",
"Complexity:\n",
"* Time: O(n) where n is the value of the input number\n",
"* Space: O(1)\n",
"\n",
"### Sieve of Eratosthenes\n",
"\n",
"The Sieve of Eratosthenes provides a more efficient way of computing and generating primes. See the challenge [\"Generate a list of primes\"]() for more details."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import math\n",
"\n",
"\n",
"class Math(object):\n",
"\n",
" def check_prime(self, num):\n",
" if num is None:\n",
" raise TypeError('num cannot be None')\n",
" if num < 2:\n",
" return False\n",
" for i in range(2, num):\n",
" if num % i == 0:\n",
" return False\n",
" return True\n",
"\n",
" def check_prime_optimized(self, num):\n",
" if num is None:\n",
" raise TypeError('num cannot be None')\n",
" if num < 2:\n",
" return False\n",
" for i in range(2, int(math.sqrt(num)+1)):\n",
" if num % i == 0:\n",
" return False\n",
" return True"
]
},
{
"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_check_prime.py\n"
]
}
],
"source": [
"%%writefile test_check_prime.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestMath(object):\n",
"\n",
" def test_check_prime(self):\n",
" math = Math()\n",
" assert_raises(TypeError, math.check_prime, None)\n",
" assert_raises(TypeError, math.check_prime, 98.6)\n",
" assert_equal(math.check_prime(0), False)\n",
" assert_equal(math.check_prime(1), False)\n",
" assert_equal(math.check_prime(97), True)\n",
" print('Success: test_check_prime')\n",
"\n",
"\n",
"def main():\n",
" test = TestMath()\n",
" test.test_check_prime()\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_check_prime\n"
]
}
],
"source": [
"%run -i test_check_prime.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 TestMath(object):
def test_check_prime(self):
math = Math()
assert_raises(TypeError, math.check_prime, None)
assert_raises(TypeError, math.check_prime, 98.6)
assert_equal(math.check_prime(0), False)
assert_equal(math.check_prime(1), False)
assert_equal(math.check_prime(97), True)
print('Success: test_check_prime')
def main():
test = TestMath()
test.test_check_prime()
if __name__ == '__main__':
main()