Skip to content

Commit 5850bb0

Browse files
committed
moved time-series, fixed corrupted ipynb in 01-Principles, refactored to solutions
1 parent 89f1636 commit 5850bb0

11 files changed

+3358
-41645
lines changed

01-Principles/02 Python Intermediate.ipynb

+19-59
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291
"\n",
292292
"A backslash character can also be used, and it tells the interpreter that the next character – following the slash – should be treated as a new line. This character is useful when you don’t want to start a new line in the text itself but in the code. \n",
293293
"\n",
294-
"### `Open()` function\n",
294+
"### Open() function\n",
295295
"\n",
296296
"This works as follows:\n",
297297
"\n",
@@ -371,7 +371,7 @@
371371
"source": [
372372
"Above we are using **iPython Magicks** to access the command line and print out writing.txt into the console. Notice that we are not actually creating new lines in the file, merely the `write()` function glues whatever you write next to the previous write statement, and we have to manually add `\\n` into the string if we want new lines to appear.\n",
373373
"\n",
374-
"### `Read()`\n",
374+
"### Read()\n",
375375
"\n",
376376
"Let's try reading in a file:"
377377
]
@@ -450,7 +450,7 @@
450450
"cell_type": "markdown",
451451
"metadata": {},
452452
"source": [
453-
"## The `with` statement\n",
453+
"## With statement\n",
454454
"\n",
455455
"You can also work with file objects using the with statement. It is designed to provide much cleaner syntax and exceptions handling when you are working with code. That explains why it’s good practice to use the with statement where applicable. \n",
456456
"\n",
@@ -526,8 +526,6 @@
526526
"source": [
527527
"## Exception Handling\n",
528528
"\n",
529-
"What happens when something happens in the code that is completely unpredictable? For instance, attempting to access a file that doesn't exist, a high-level interrupt from the operating system or passing the wrong type of parameter to a function. The way programs handle these sorts of errors is through **Exception Handling**.\n",
530-
"\n",
531529
" Exceptions handling in Python is very similar to Java. The code, which harbours the risk of an exception, is embedded in a try block. But whereas in Java exceptions are caught by catch clauses, we have statements introduced by an \"except\" keyword in Python. It's possible to create \"custom-made\" exceptions: With the raise statement it's possible to force a specified exception to occur.\n",
532530
"\n",
533531
"Let's look at a simple example. Assuming we want to ask the user to enter an integer number. If we use a input(), the input will be a string, which we have to cast into an integer. If the input has not been a valid integer, we will generate (raise) a ValueError. We show this in the following interactive session: "
@@ -634,7 +632,7 @@
634632
"\n",
635633
"## Arbitrary Parameter Lengths\n",
636634
"\n",
637-
"There are many situations in programming, in which the exact number of necessary parameters cannot be determined _a priori_. An arbitrary parameter number can be accomplished in Python with so-called tuple references. An asterisk `*` is used in front of the last parameter name to denote it as a tuple reference. This asterisk shouldn't be mistaken with the C syntax, where this notation is connected with pointers.\n",
635+
"There are many situations in programming, in which the exact number of necessary parameters cannot be determined a-priori. An arbitrary parameter number can be accomplished in Python with so-called tuple references. An asterisk `*` is used in front of the last parameter name to denote it as a tuple reference. This asterisk shouldn't be mistaken with the C syntax, where this notation is connected with pointers.\n",
638636
"Example:"
639637
]
640638
},
@@ -705,7 +703,7 @@
705703
"source": [
706704
"### Arbitrary Number of 'Keyword' Parameters\n",
707705
"\n",
708-
"In the previous section we demonstrated how to pass an arbitrary number of positional parameters to a function. It is also possible to pass an arbitrary number of keyword parameters to a function. To this purpose, we have to use the double asterisk `**`: "
706+
"In the previous chapter we demonstrated how to pass an arbitrary number of positional parameters to a function. It is also possible to pass an arbitrary number of keyword parameters to a function. To this purpose, we have to use the double asterisk `**`: "
709707
]
710708
},
711709
{
@@ -1019,7 +1017,7 @@
10191017
},
10201018
{
10211019
"cell_type": "code",
1022-
"execution_count": null,
1020+
"execution_count": 1,
10231021
"metadata": {},
10241022
"outputs": [],
10251023
"source": [
@@ -1037,7 +1035,7 @@
10371035
},
10381036
{
10391037
"cell_type": "code",
1040-
"execution_count": null,
1038+
"execution_count": 2,
10411039
"metadata": {},
10421040
"outputs": [],
10431041
"source": [
@@ -1050,47 +1048,12 @@
10501048
"source": [
10511049
"## Task 3\n",
10521050
"\n",
1053-
"Write the function `normalize` that takes **AT LEAST** two parameters (i.e an arbitrary number) as input:\n",
1054-
"\n",
1055-
"1. A real number ($A$)\n",
1056-
"2. A list of arbitrary length $B$\n",
1057-
"\n",
1058-
"and divides the $A$ by each element $B$ in the list, then returns a `list` of these values. Make sure you include error checking/exception handling in your function.\n",
1059-
"\n",
1060-
"$A$ can be any real floating-point of your choice. Make use of the following list $B$ to test against, including lists of your own."
1061-
]
1062-
},
1063-
{
1064-
"cell_type": "code",
1065-
"execution_count": null,
1066-
"metadata": {},
1067-
"outputs": [],
1068-
"source": [
1069-
"from math import sin, inf, nan, cos\n",
1070-
"B = [1., 4.999, inf, sin(2.33333), 0., 3.55, nan, .0009, cos(0.)]"
1071-
]
1072-
},
1073-
{
1074-
"cell_type": "code",
1075-
"execution_count": null,
1076-
"metadata": {},
1077-
"outputs": [],
1078-
"source": [
1079-
"# your codes here"
1080-
]
1081-
},
1082-
{
1083-
"cell_type": "markdown",
1084-
"metadata": {},
1085-
"source": [
1086-
"## Task 4\n",
1087-
"\n",
10881051
"Write a function `log_sum_product(first, *vals)` that accepts a *arbitrary* number of numbers.\n",
10891052
"\n",
1090-
"Calculating the **product** of a set of probabilities can lead to numerical instability. To get around this, we can convert using `log` and `sum` all of the values in logspace, then convert back using `exp`.\n",
1053+
"Calculating the **product** of a set of proabilities can lead to numerical instability, particularly for values less than 1 (in a probabilistic context). To get around this, we can convert to `log` and `sum` all of the values in logspace, then convert back using `exp`.\n",
10911054
"\n",
10921055
"$$\n",
1093-
"P(\\vec{x})=\\prod_{i=1}^n \\vec{x}_i= \\exp \\left(\\sum_{i=1}^n \\log(\\vec{x}_i)\\right)\n",
1056+
"P(\\vec{x})=\\exp \\left(\\sum_{i=1}^n \\log(\\vec{x}_i)\\right)\n",
10941057
"$$\n",
10951058
"\n",
10961059
"When summing the values together, we recommend you use `math.fsum` rather than using the default Python `sum` function.\n",
@@ -1100,26 +1063,27 @@
11001063
},
11011064
{
11021065
"cell_type": "code",
1103-
"execution_count": null,
1066+
"execution_count": 3,
11041067
"metadata": {},
11051068
"outputs": [],
11061069
"source": [
1107-
"# your codes here"
1070+
"# your codes here\n",
1071+
"import math"
11081072
]
11091073
},
11101074
{
11111075
"cell_type": "markdown",
11121076
"metadata": {},
11131077
"source": [
1114-
"## Task 5\n",
1078+
"## Task 4\n",
11151079
"\n",
11161080
"Below is a method from numerical integration for approximating a definite integral:\n",
11171081
"\n",
11181082
"$$\n",
11191083
"\\int_a^b f(x)dx \\approx \\sum_{k=1}^N \\frac{f(x_{k-1})+f(x_k)}{2}\\Delta x_k \n",
11201084
"$$\n",
11211085
"\n",
1122-
"Write a function `f(x)` using **lambda notation** which returns:\n",
1086+
"Write a function `f(x)` which returns:\n",
11231087
"\n",
11241088
"$$\n",
11251089
"f(x)=\\cos x\n",
@@ -1141,7 +1105,7 @@
11411105
},
11421106
{
11431107
"cell_type": "code",
1144-
"execution_count": null,
1108+
"execution_count": 4,
11451109
"metadata": {},
11461110
"outputs": [],
11471111
"source": [
@@ -1150,7 +1114,7 @@
11501114
},
11511115
{
11521116
"cell_type": "code",
1153-
"execution_count": null,
1117+
"execution_count": 5,
11541118
"metadata": {},
11551119
"outputs": [],
11561120
"source": [
@@ -1177,7 +1141,7 @@
11771141
},
11781142
{
11791143
"cell_type": "code",
1180-
"execution_count": null,
1144+
"execution_count": 6,
11811145
"metadata": {},
11821146
"outputs": [],
11831147
"source": [
@@ -1187,7 +1151,7 @@
11871151
],
11881152
"metadata": {
11891153
"kernelspec": {
1190-
"display_name": "Python [default]",
1154+
"display_name": "Python 3",
11911155
"language": "python",
11921156
"name": "python3"
11931157
},
@@ -1201,11 +1165,7 @@
12011165
"name": "python",
12021166
"nbconvert_exporter": "python",
12031167
"pygments_lexer": "ipython3",
1204-
<<<<<<< HEAD
1205-
"version": "3.5.6"
1206-
=======
1207-
"version": "3.7.0"
1208-
>>>>>>> ac3d256233ccac1dff220a9181e2211e4ff0a14d
1168+
"version": "3.6.7"
12091169
}
12101170
},
12111171
"nbformat": 4,

0 commit comments

Comments
 (0)