Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev 02 #150

Open
wants to merge 22 commits into
base: andrea_deanseris
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .ipynb_checkpoints/01_Fundamentls-checkpoint.ipynb

This file was deleted.

6 changes: 0 additions & 6 deletions .ipynb_checkpoints/02_NumberRepresentation-checkpoint.ipynb

This file was deleted.

110 changes: 79 additions & 31 deletions 01_Fundamentals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1]\n",
"[1, 1]\n",
"[9, 9, 9, 1]\n",
"[1, 1, 1]\n",
"[1, 1, 1, 1]\n"
]
}
],
"source": [
"def f(x = []):\n",
" x.append(1)\n",
Expand All @@ -192,9 +204,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1]\n",
"[1]\n",
"[9, 9, 9, 1]\n",
"[1]\n",
"[1]\n"
]
}
],
"source": [
"def f(x = None):\n",
" if x is None:\n",
Expand Down Expand Up @@ -350,7 +374,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -367,9 +391,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\r\n",
"World\r\n"
]
}
],
"source": [
"! cat 'foo.txt'"
]
Expand Down Expand Up @@ -405,7 +438,7 @@
"metadata": {},
"outputs": [],
"source": [
"for i in map(lambda x: x*x+3*x+1, range(5)): print (i)"
"for i in map(lambda x: x*x, range(5)): print (i)"
]
},
{
Expand Down Expand Up @@ -439,7 +472,7 @@
" # base case\n",
" if n==0 or n==1:\n",
" return 1\n",
" # recurssive case\n",
" # recurssive caae\n",
" else:\n",
" return fib1(n-1) + fib1(n-2)\n",
"\n",
Expand Down Expand Up @@ -526,9 +559,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<generator object <genexpr> at 0x7fa83048c6e0>\n",
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
"{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}\n"
]
}
],
"source": [
"# A generator expression\n",
"\n",
Expand Down Expand Up @@ -578,7 +622,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -589,23 +633,27 @@
" print(\"Something is happening after the function is called.\")\n",
" return wrapper\n",
"\n",
"@my_decorator\n",
"def say_whee():\n",
" print(\"Whee!\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"@my_decorator is just an easier way of saying say_whee = my_decorator(say_whee). It’s how you apply a decorator to a function."
" print(\"Whee!\")\n",
"\n",
"say_whee = my_decorator(say_whee)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Something is happening before the function is called.\n",
"Whee!\n",
"Something is happening after the function is called.\n"
]
}
],
"source": [
"say_whee()"
]
Expand Down Expand Up @@ -685,14 +733,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
"pygments_lexer": "ipython2",
"version": "2.7.15rc1"
}
},
"nbformat": 4,
Expand Down
46 changes: 24 additions & 22 deletions 01ex_Fundamentals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]\n",
"[0, 4, 16]\n"
]
}
],
"source": [
"# 1\n",
"ans = []\n",
Expand All @@ -30,14 +39,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"2\\. Convert the following function into a pure function with no global variables or side effects"
"2\\. Converte the following function into a pure function with no global variables or side effects"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 0, 1, 2, 3, 4]\n",
"[1, 2, 3, 0, 1, 2, 3, 4]\n"
]
}
],
"source": [
"x = 5\n",
"def f(alist):\n",
Expand Down Expand Up @@ -76,23 +94,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"5\\. Use HOFs (zip in particular) to compute the weight of a circle, a disk and a sphere, assuming different radii and different densities:\n",
"\n",
"```python\n",
"densities = {\"Al\":[0.5,1,2],\"Fe\":[3,4,5],\"Pb\": [15,20,30]}\n",
"radii = [1,2,3]\n",
"```\n",
"\n",
"where the entries of the dictionary's values are the linear, superficial and volumetric densities of the materials respectively.\n",
"\n",
"In particular define a list of three lambda functions using a comprehension that computes the circumference, the area and the volume for a given radius.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6\\. Edit the class defintion to add an instance attribute of is_hungry = True to the Dog class. Then add a method called eat() which changes the value of is_hungry to False when called. Figure out the best way to feed each dog and then output “My dogs are hungry.” if all are hungry or “My dogs are not hungry.” if all are not hungry. The final output should look like this:\n",
"5\\. Edit the class defintion to add an instance attribute of is_hungry = True to the Dog class. Then add a method called eat() which changes the value of is_hungry to False when called. Figure out the best way to feed each dog and then output “My dogs are hungry.” if all are hungry or “My dogs are not hungry.” if all are not hungry. The final output should look like this:\n",
"\n",
"`I have 3 dogs. \n",
"Tom is 6. \n",
Expand Down
119 changes: 119 additions & 0 deletions 02ex_NumberRepresentation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1\\. Write a function that converts number representation (bin<->dec<->hex)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2\\. Write a function that converts a 32 bit word into a single precision floating point (i.e. interprets the various bits as sign, mantissa and exponent)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3\\. Write a program to determine the underflow and overflow limits (within a factor of 2) for python on your computer. \n",
"\n",
"**Tips**: define two variables inizialized to 1 and halve/double them enough time to exceed the under/over-flow limits "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4\\. Write a program to determine the machine precision\n",
"\n",
"**Tips**: define a new variable by adding a smaller and smaller value (proceeding similarly to prob. 2) to an original variable and check the point where the two are the same "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5\\. Write a function that takes in input three parameters $a$, $b$ and $c$ and prints out the two solutions to the quadratic equation $ax^2+bx+c=0$ using the standard formula:\n",
"$$\n",
"x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}\n",
"$$\n",
"\n",
"(a) use the program to compute the solution for $a=0.001$, $b=1000$ and $c=0.001$\n",
"\n",
"(b) re-express the standard solution formula by multiplying top and bottom by $-b\\mp\\sqrt{b^2-4ac}$ and again find the solution for $a=0.001$, $b=1000$ and $c=0.001$. How does it compare with what previously obtained? Why?\n",
"\n",
"(c) write a function that compute the roots of a quadratic equation accurately in all cases"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6\\. Write a program that implements the function $f(x)=x(x−1)$\n",
"\n",
"(a) Calculate the derivative of the function at the point $x = 1$ using the derivative definition:\n",
"\n",
"$$\n",
"\\frac{{\\rm d}f}{{\\rm d}x} = \\lim_{\\delta\\to0} \\frac{f(x+\\delta)-f(x)}{\\delta}\n",
"$$\n",
"\n",
"with $\\delta = 10^{−2}$. Calculate the true value of the same derivative analytically and compare with the answer your program gives. The two will not agree perfectly. Why not?\n",
"\n",
"(b) Repeat the calculation for $\\delta = 10^{−4}, 10^{−6}, 10^{−8}, 10^{−10}, 10^{−12}$ and $10^{−14}$. How does the accuracy scales with $\\delta$?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7\\. Consider the integral of the semicircle of radius 1:\n",
"$$\n",
"I=\\int_{-1}^{1} \\sqrt(1-x^2) {\\rm d}x\n",
"$$\n",
"which it's known to be $I=\\frac{\\pi}{2}=1.57079632679...$.\n",
"Alternatively we can use the Riemann definition of the integral:\n",
"$$\n",
"I=\\lim_{N\\to\\infty} \\sum_{k=1}^{N} h y_k \n",
"$$\n",
"\n",
"with $h=2/N$ the width of each of the $N$ slices the domain is divided into and where\n",
"$y_k$ is the value of the function at the $k-$th slice.\n",
"\n",
"(a) Write a programe to compute the integral with $N=100$. How does the result compares to the true value?\n",
"\n",
"(b) How much can $N$ be increased if the computation needs to be run in less than a second? What is the gain in running it for 1 minute? \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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading