mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
208 lines
5.0 KiB
Python
208 lines
5.0 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: 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
|
|
}
|