interactive-coding-challenges/math_probability/generate_primes/check_prime_challenge.ipynb

166 lines
4.2 KiB
Python
Raw Permalink Normal View History

2017-03-29 16:45:04 +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": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Generate a list of primes.\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",
"* 20 -> [False, False, True, True, False, True, False, True, False, False, False, True, False, True, False, False, False, True, False, True]"
2017-03-29 16:45:04 +08:00
]
},
{
"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": 3,
"metadata": {},
2017-03-29 16:45:04 +08:00
"outputs": [],
"source": [
"class PrimeGenerator(object):\n",
"\n",
" def generate_primes(self, max_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": {},
2017-03-29 16:45:04 +08:00
"outputs": [],
"source": [
"# %load test_generate_primes.py\n",
"import unittest\n",
2017-03-29 16:45:04 +08:00
"\n",
"\n",
"class TestMath(unittest.TestCase):\n",
2017-03-29 16:45:04 +08:00
"\n",
" def test_generate_primes(self):\n",
" prime_generator = PrimeGenerator()\n",
" self.assertRaises(TypeError, prime_generator.generate_primes, None)\n",
" self.assertRaises(TypeError, prime_generator.generate_primes, 98.6)\n",
" self.assertEqual(prime_generator.generate_primes(20), [False, False, True, \n",
2017-03-29 16:45:04 +08:00
" True, False, True, \n",
" False, True, False, \n",
" False, False, True, \n",
" False, True, False, \n",
" False, False, True, \n",
" False, True])\n",
" print('Success: generate_primes')\n",
"\n",
"\n",
"def main():\n",
" test = TestMath()\n",
" test.test_generate_primes()\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.7.2"
2017-03-29 16:45:04 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 1
2017-03-29 16:45:04 +08:00
}