diff --git a/lectures/functions.md b/lectures/functions.md index c0922562..8fc9cc78 100644 --- a/lectures/functions.md +++ b/lectures/functions.md @@ -513,7 +513,7 @@ Try to use lambda expressions to define the function `f`. :class: dropdown ``` -- Here's one solution for part 1 +Here's one solution for part 1 ```{code-cell} python3 def factorial(n): @@ -525,7 +525,7 @@ def factorial(n): factorial(4) ``` -- Adding the lambda expression +Adding the lambda expression ```{code-cell} python3 def factorial(n,f = lambda x: x): @@ -561,14 +561,21 @@ The [binomial random variable](https://en.wikipedia.org/wiki/Binomial_distributi Without any import besides `from numpy.random import uniform`, write a function `binomial_rv` such that `binomial_rv(n, p)` generates one draw of $Y$. -Hint: If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` evaluates to `True` with probability $p$. +```{hint} +:class: dropdown + +If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` evaluates to `True` with probability $p$. +``` + ```{exercise-end} ``` ```{solution-start} func_ex2 :class: dropdown -```` +``` + +Here is one solution: ```{code-cell} python3 from numpy.random import uniform diff --git a/lectures/getting_started.md b/lectures/getting_started.md index bb7e516b..d899049d 100644 --- a/lectures/getting_started.md +++ b/lectures/getting_started.md @@ -247,7 +247,9 @@ When you're ready to execute the code in a cell, hit `Shift-Enter` instead of th :figclass: auto ``` -(Note: There are also menu and button options for running code in a cell that you can find by exploring) +```{note} +There are also menu and button options for running code in a cell that you can find by exploring. +``` #### Modal Editing diff --git a/lectures/numba.md b/lectures/numba.md index 30094fa6..d9793ef9 100644 --- a/lectures/numba.md +++ b/lectures/numba.md @@ -478,7 +478,7 @@ When Numba compiles machine code for functions, it treats global variables as co ```{exercise} :label: speed_ex1 -{ref}`Previously ` we considered how to approximate $\pi$ by +{ref}`Previously ` we considered how to approximate $\pi$ by Monte Carlo. Use the same idea here, but make the code efficient using Numba. @@ -560,11 +560,15 @@ To test your code, evaluate the fraction of time that the chain spends in the lo If your code is correct, it should be about 2/3. -Hints: + +```{hint} +:class: dropdown * Represent the low state as 0 and the high state as 1. * If you want to store integers in a NumPy array and then apply JIT compilation, use `x = np.empty(n, dtype=np.int_)`. +``` + ```{exercise-end} ``` diff --git a/lectures/numpy.md b/lectures/numpy.md index 7d2a14bc..ab6b819b 100644 --- a/lectures/numpy.md +++ b/lectures/numpy.md @@ -1189,8 +1189,10 @@ Now write a new function that does the same job, but uses NumPy arrays and array (Such functionality is already implemented as `np.poly1d`, but for the sake of the exercise don't use this class) -* Hint: Use `np.cumprod()` - +```{hint} +:class: dropdown +Use `np.cumprod()` +``` ```{exercise-end} ``` @@ -1262,7 +1264,12 @@ It helps to sketch the intervals on paper. Your exercise is to speed it up using NumPy, avoiding explicit loops -* Hint: Use `np.searchsorted` and `np.cumsum` +```{hint} +:class: dropdown + +Use `np.searchsorted` and `np.cumsum` + +``` If you can, implement the functionality as a class called `DiscreteRV`, where diff --git a/lectures/pandas.md b/lectures/pandas.md index 71b846ea..c30b5e41 100644 --- a/lectures/pandas.md +++ b/lectures/pandas.md @@ -321,10 +321,12 @@ A trivial example is to return itself for each row in the dataframe df.apply(lambda row: row, axis=1) ``` -Note: for the `.apply()` method +```{note} +For the `.apply()` method - axis = 0 -- apply function to each column (variables) - axis = 1 -- apply function to each row (observations) - axis = 0 is the default parameter +``` We can use it together with `.loc[]` to do some more advanced selection. diff --git a/lectures/python_essentials.md b/lectures/python_essentials.md index cf85b8a1..b1eb7708 100644 --- a/lectures/python_essentials.md +++ b/lectures/python_essentials.md @@ -595,11 +595,10 @@ all([1 <= 2 <= 3, "a" in "letter"]) any([1 <= 2 <= 3, "a" in "letter"]) ``` -Note: - +```{note} * `all()` returns `True` when *all* boolean values/expressions in the sequence are `True` * `any()` returns `True` when *any* boolean values/expressions in the sequence are `True` - +``` ## Coding Style and Documentation @@ -692,20 +691,28 @@ Solve the following exercises. (For some, the built-in function `sum()` comes in handy). -```{exercise} +```{exercise-start} :label: pyess_ex1 - +``` Part 1: Given two numeric lists or tuples `x_vals` and `y_vals` of equal length, compute their inner product using `zip()`. Part 2: In one line, count the number of even numbers in 0,...,99. -* Hint: `x % 2` returns 0 if `x` is even, 1 otherwise. - Part 3: Given `pairs = ((2, 5), (4, 2), (9, 8), (12, 10))`, count the number of pairs `(a, b)` such that both `a` and `b` are even. + +```{hint} +:class: dropdown + +`x % 2` returns 0 if `x` is even, 1 otherwise. + ``` +```{exercise-end} +``` + + ```{solution-start} pyess_ex1 :class: dropdown ``` @@ -804,12 +811,20 @@ p(1, (2, 4)) ``` -```{exercise} +```{exercise-start} :label: pyess_ex3 +``` Write a function that takes a string as an argument and returns the number of capital letters in the string. -Hint: `'foo'.upper()` returns `'FOO'`. +```{hint} +:class: dropdown + +`'foo'.upper()` returns `'FOO'`. + +``` + +```{exercise-end} ``` ```{solution-start} pyess_ex3