Skip to content

Instantly share code, notes, and snippets.

@davipatti
Created April 19, 2023 18:04
Show Gist options
  • Save davipatti/019a15d9b194cd8412ef50ffa0a33c00 to your computer and use it in GitHub Desktop.
Save davipatti/019a15d9b194cd8412ef50ffa0a33c00 to your computer and use it in GitHub Desktop.
[ipynb toggle code demo] #jupyter #ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# ipynb toggle code demo\n",
"\n",
"Toggle code cells by clicking the link below."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script>\n",
"code_show = true;\n",
"function code_toggle() {\n",
" if (code_show) {\n",
" $('div.input').hide();\n",
" } else {\n",
" $('div.input').show();\n",
" } code_show = !code_show\n",
"} $(document).ready(code_toggle);\n",
"</script>\n",
"<font size='2'><a href='javascript:code_toggle()'>Toggle code.</a></font>\n",
"<hr />\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"\n",
"HTML(\"\"\"\n",
"<script>\n",
"code_show = true;\n",
"function code_toggle() {\n",
" if (code_show) {\n",
" $('div.input').hide();\n",
" } else {\n",
" $('div.input').show();\n",
" } code_show = !code_show\n",
"} $(document).ready(code_toggle);\n",
"</script>\n",
"<font size='2'><a href='javascript:code_toggle()'>Toggle code.</a></font>\n",
"<hr />\n",
"\"\"\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"The first 50 Fibonacci numbers:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0\n",
"1 1\n",
"2 1\n",
"3 2\n",
"4 3\n",
"5 5\n",
"6 8\n",
"7 13\n",
"8 21\n",
"9 34\n",
"10 55\n",
"11 89\n",
"12 144\n",
"13 233\n",
"14 377\n",
"15 610\n",
"16 987\n",
"17 1597\n",
"18 2584\n",
"19 4181\n",
"20 6765\n",
"21 10946\n",
"22 17711\n",
"23 28657\n",
"24 46368\n",
"25 75025\n",
"26 121393\n",
"27 196418\n",
"28 317811\n",
"29 514229\n",
"30 832040\n",
"31 1346269\n",
"32 2178309\n",
"33 3524578\n",
"34 5702887\n",
"35 9227465\n",
"36 14930352\n",
"37 24157817\n",
"38 39088169\n",
"39 63245986\n",
"40 102334155\n",
"41 165580141\n",
"42 267914296\n",
"43 433494437\n",
"44 701408733\n",
"45 1134903170\n",
"46 1836311903\n",
"47 2971215073\n",
"48 4807526976\n",
"49 7778742049\n"
]
}
],
"source": [
"from functools import lru_cache\n",
"\n",
"\n",
"@lru_cache\n",
"def fib(n: int) -> int:\n",
" \"\"\"The nth Fibonacci number\"\"\"\n",
" if n == 0:\n",
" return 0\n",
" elif n == 1:\n",
" return 1\n",
" else:\n",
" return fib(n - 1) + fib(n - 2)\n",
"\n",
"for n in range(50):\n",
" print(n, fib(n))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.10.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment