Html pages no include tags and completion date.
This commit is contained in:
@@ -33,7 +33,9 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assert(get_sum_of_natural_dividable_by_3_and_5_below(10) == 23)"
|
||||
@@ -42,7 +44,9 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -58,6 +62,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Tue, 19 Aug 2014, 20:11",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
@@ -73,8 +78,11 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.3"
|
||||
}
|
||||
"version": "3.5.4"
|
||||
},
|
||||
"tags": [
|
||||
"brute_force"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
|
||||
@@ -138,6 +138,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Tue, 19 Aug 2014, 20:36",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
|
||||
@@ -176,6 +176,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Tue, 19 Aug 2014, 23:35",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
|
||||
@@ -150,6 +150,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 13:27",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
@@ -169,7 +170,9 @@
|
||||
},
|
||||
"tags": [
|
||||
"palindrome",
|
||||
"combinations"
|
||||
"combinations",
|
||||
"brute force",
|
||||
"timeit"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -107,6 +107,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 14:32",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 15:13",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
@@ -87,7 +88,8 @@
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.4"
|
||||
}
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
|
||||
@@ -64,6 +64,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 15:40",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
|
||||
311
ipython/EulerProblem008.html
Normal file
311
ipython/EulerProblem008.html
Normal file
File diff suppressed because one or more lines are too long
144
ipython/EulerProblem008.ipynb
Normal file
144
ipython/EulerProblem008.ipynb
Normal file
@@ -0,0 +1,144 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 8\n",
|
||||
"\n",
|
||||
"The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.\n",
|
||||
"\n",
|
||||
"~~~\n",
|
||||
"73167176531330624919225119674426574742355349194934\n",
|
||||
"96983520312774506326239578318016984801869478851843\n",
|
||||
"85861560789112949495459501737958331952853208805511\n",
|
||||
"12540698747158523863050715693290963295227443043557\n",
|
||||
"66896648950445244523161731856403098711121722383113\n",
|
||||
"62229893423380308135336276614282806444486645238749\n",
|
||||
"30358907296290491560440772390713810515859307960866\n",
|
||||
"70172427121883998797908792274921901699720888093776\n",
|
||||
"65727333001053367881220235421809751254540594752243\n",
|
||||
"52584907711670556013604839586446706324415722155397\n",
|
||||
"53697817977846174064955149290862569321978468622482\n",
|
||||
"83972241375657056057490261407972968652414535100474\n",
|
||||
"82166370484403199890008895243450658541227588666881\n",
|
||||
"16427171479924442928230863465674813919123162824586\n",
|
||||
"17866458359124566529476545682848912883142607690042\n",
|
||||
"24219022671055626321111109370544217506941658960408\n",
|
||||
"07198403850962455444362981230987879927244284909188\n",
|
||||
"84580156166097919133875499200524063689912560717606\n",
|
||||
"05886116467109405077541002256983155200055935729725\n",
|
||||
"71636269561882670428252483600823257530420752963450\n",
|
||||
"~~~\n",
|
||||
"\n",
|
||||
"Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We need the number as a list of integers first."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def remove_newlines(s):\n",
|
||||
" import re\n",
|
||||
" return re.sub(r'\\n', '', s)\n",
|
||||
"\n",
|
||||
"digits_string = \"\"\"73167176531330624919225119674426574742355349194934\n",
|
||||
"96983520312774506326239578318016984801869478851843\n",
|
||||
"85861560789112949495459501737958331952853208805511\n",
|
||||
"12540698747158523863050715693290963295227443043557\n",
|
||||
"66896648950445244523161731856403098711121722383113\n",
|
||||
"62229893423380308135336276614282806444486645238749\n",
|
||||
"30358907296290491560440772390713810515859307960866\n",
|
||||
"70172427121883998797908792274921901699720888093776\n",
|
||||
"65727333001053367881220235421809751254540594752243\n",
|
||||
"52584907711670556013604839586446706324415722155397\n",
|
||||
"53697817977846174064955149290862569321978468622482\n",
|
||||
"83972241375657056057490261407972968652414535100474\n",
|
||||
"82166370484403199890008895243450658541227588666881\n",
|
||||
"16427171479924442928230863465674813919123162824586\n",
|
||||
"17866458359124566529476545682848912883142607690042\n",
|
||||
"24219022671055626321111109370544217506941658960408\n",
|
||||
"07198403850962455444362981230987879927244284909188\n",
|
||||
"84580156166097919133875499200524063689912560717606\n",
|
||||
"05886116467109405077541002256983155200055935729725\n",
|
||||
"71636269561882670428252483600823257530420752963450\"\"\"\n",
|
||||
"\n",
|
||||
"digits = [int(d) for d in remove_newlines(digits_string)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" Then we can use slicing to do a brute force."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"23514624000\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def product(xs):\n",
|
||||
" from operator import mul\n",
|
||||
" from functools import reduce\n",
|
||||
" return reduce(mul, xs, 1)\n",
|
||||
"\n",
|
||||
"def get_largest_product_of_n_digits(digits, n):\n",
|
||||
" return max((product(digits[i:i + n]) for i in range(len(digits))))\n",
|
||||
"\n",
|
||||
"assert(get_largest_product_of_n_digits(digits, 4) == 5832)\n",
|
||||
"assert(get_largest_product_of_n_digits(digits, 13) == 23514624000)\n",
|
||||
"print(get_largest_product_of_n_digits(digits, 13))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 16:03",
|
||||
"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.4"
|
||||
},
|
||||
"tags": [
|
||||
"product",
|
||||
"reduce",
|
||||
"slice"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
353
ipython/EulerProblem009.html
Normal file
353
ipython/EulerProblem009.html
Normal file
File diff suppressed because one or more lines are too long
165
ipython/EulerProblem009.ipynb
Normal file
165
ipython/EulerProblem009.ipynb
Normal file
@@ -0,0 +1,165 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 9\n",
|
||||
"\n",
|
||||
"A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which,\n",
|
||||
"\n",
|
||||
"$a^2 + b^2 = c^2$\n",
|
||||
"\n",
|
||||
"For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$.\n",
|
||||
"\n",
|
||||
"There exists exactly one Pythagorean triplet for which $a + b + c = 1000$.\n",
|
||||
"Find the product $abc$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We start bruteforcing even though it feels like there may be a smart solution."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"66.96907186399949\n",
|
||||
"31875000\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import timeit\n",
|
||||
"\n",
|
||||
"def brute_force():\n",
|
||||
" for a in range(1, 251):\n",
|
||||
" for b in range(a + 1, 501):\n",
|
||||
" for c in range(b + 1, 1001):\n",
|
||||
" if a + b + c == 1000 and a * a + b * b == c * c:\n",
|
||||
" return a * b * c\n",
|
||||
"\n",
|
||||
"print(timeit.timeit(brute_force, number=10))\n",
|
||||
"print(brute_force())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's do some optimization by cancelling the loops when we exceed the boundaries. Actually, I have also realized that choosing 251 and 501 is a little bit arbitrary. For example if a, b, c where something like $332, 333, 335$ that could be a solution and the first range was too low. Then again, we would have realized that as soon as we get back None, so it is okay."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"64.4656584559998\n",
|
||||
"31875000\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def brute_force():\n",
|
||||
" for a in range(1, 251):\n",
|
||||
" for b in range(a + 1, 501):\n",
|
||||
" if a + b > 600:\n",
|
||||
" break\n",
|
||||
" for c in range(b + 1, 1001):\n",
|
||||
" if a + b + c == 1000 and a * a + b * b == c * c:\n",
|
||||
" return a * b * c\n",
|
||||
" \n",
|
||||
"print(timeit.timeit(brute_force, number=10))\n",
|
||||
"print(brute_force())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Big time save. Kappa. Okay, I am stupid. If I have a and b I can calculate c and check if it is a solution."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.22822808900036762\n",
|
||||
"31875000\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def smart_brute_force():\n",
|
||||
" for a in range(1, 251):\n",
|
||||
" for b in range(a + 1, 501):\n",
|
||||
" c = 1000 - a - b\n",
|
||||
" if a * a + b * b == c * c:\n",
|
||||
" return a * b * c\n",
|
||||
" \n",
|
||||
"print(timeit.timeit(smart_brute_force, number=10))\n",
|
||||
"print(smart_brute_force()) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 17:06",
|
||||
"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.4"
|
||||
},
|
||||
"tags": [
|
||||
"brute force",
|
||||
"pythagorean",
|
||||
"triplet"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
193
ipython/EulerProblem010.html
Normal file
193
ipython/EulerProblem010.html
Normal file
File diff suppressed because one or more lines are too long
37
ipython/EulerProblem010.ipynb
Normal file
37
ipython/EulerProblem010.ipynb
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 10\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Wed, 20 Aug 2014, 20:28",
|
||||
"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.4"
|
||||
},
|
||||
"tags": [
|
||||
"primes"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -4,15 +4,26 @@ import subprocess
|
||||
from operator import itemgetter
|
||||
from collections import namedtuple
|
||||
from os.path import getmtime
|
||||
import json
|
||||
|
||||
|
||||
def extract_metadata(ipynb_file):
|
||||
Metadata = namedtuple('Metadata', ['tags', 'completion_date'])
|
||||
with open(ipynb_file, 'r') as f:
|
||||
j = json.load(f)
|
||||
tags = j['metadata']['tags']
|
||||
completion_date = j['metadata']['completion_date']
|
||||
return Metadata(tags, completion_date)
|
||||
|
||||
|
||||
def file_name_to_solution(name):
|
||||
Solution = namedtuple('Solution', ["number", "ipynb", "html", "name"])
|
||||
Solution = namedtuple('Solution', ["number", "ipynb", "html", "name", "metadata"])
|
||||
number = int(name.replace("EulerProblem", "").replace(".ipynb", ""))
|
||||
ipynb = name
|
||||
metadata = extract_metadata(ipynb)
|
||||
html = name.replace(".ipynb", ".html")
|
||||
name = name.replace("EulerProblem", "Problem ").replace(".ipynb", "")
|
||||
return Solution(number, ipynb, html, name)
|
||||
name = name.replace("EulerProblem", "Problem ").replace(".ipynb", "")
|
||||
return Solution(number, ipynb, html, name, metadata)
|
||||
|
||||
|
||||
def get_solution_list(directory="./"):
|
||||
@@ -27,7 +38,7 @@ def convert_solutions_to_html(solutions):
|
||||
if getmtime(s.html) < getmtime(s.ipynb):
|
||||
args = ["ipython", "nbconvert", s.ipynb]
|
||||
subprocess.call(args)
|
||||
|
||||
|
||||
|
||||
def render_solutions(solutions):
|
||||
loader = jinja2.FileSystemLoader(searchpath="./")
|
||||
@@ -56,4 +67,4 @@ if __name__ == "__main__":
|
||||
solutions = get_solution_list()
|
||||
convert_solutions_to_html(solutions)
|
||||
render_solutions(solutions)
|
||||
ship_to_failx()
|
||||
#ship_to_failx()
|
||||
|
||||
@@ -27,11 +27,30 @@
|
||||
|
||||
<div class="row" style="padding-top: 40px;">
|
||||
<div class="col">
|
||||
<ul>
|
||||
{% for s in solutions %}
|
||||
<li><a href="{{s.html}}">{{s.name}}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Name</th>
|
||||
<th scope="col">Complete on</th>
|
||||
<th scope="col">Link</th>
|
||||
<th scope="col">Tags</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for s in solutions %}
|
||||
<tr>
|
||||
<td>{{s.name}}</td>
|
||||
<td>{{s.metadata.completion_date}}</td>
|
||||
<td><a href="{{s.html}}">{{s.name}}</a></td>
|
||||
<td>
|
||||
{% for t in s.metadata.tags %}
|
||||
<kbd>{{t}}</kbd>
|
||||
{% endfor %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user