You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Try to use lambda expressions to define the function `f`.
454
507
508
+
```{exercise-end}
455
509
```
456
510
457
-
```{solution-start} exercise_1
458
-
:label: solution_1
511
+
512
+
```{solution-start} func_ex1
459
513
:class: dropdown
460
514
```
461
515
@@ -498,19 +552,21 @@ factorial(2, f) # even (equivalent to factorial(5))
498
552
```
499
553
500
554
501
-
```{exercise}
502
-
:label: exercise_2
555
+
```{exercise-start}
556
+
:label: func_ex2
557
+
```
503
558
504
559
The [binomial random variable](https://en.wikipedia.org/wiki/Binomial_distribution) $Y \sim Bin(n, p)$ represents the number of successes in $n$ binary trials, where each trial succeeds with probability $p$.
505
560
506
561
Without any import besides `from numpy.random import uniform`, write a function
507
562
`binomial_rv` such that `binomial_rv(n, p)` generates one draw of $Y$.
508
563
509
564
Hint: If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` evaluates to `True` with probability $p$.
565
+
```{exercise-end}
510
566
```
511
567
512
-
```{solution-start} exercise_2
513
-
:label: solution_2
568
+
569
+
```{solution-start} func_ex2
514
570
:class: dropdown
515
571
````
516
572
@@ -532,8 +588,9 @@ binomial_rv(10, 0.5)
532
588
```
533
589
534
590
535
-
```{exercise}
536
-
:label: exercise_3
591
+
```{exercise-start}
592
+
:label: func_ex3
593
+
```
537
594
538
595
First, write a function that returns one realization of the following random device
539
596
@@ -546,14 +603,18 @@ Second, write another function that does the same task except that the second ru
546
603
- If a head occurs `k` or more times within this sequence, pay one dollar.
547
604
548
605
Use no import besides `from numpy.random import uniform`.
606
+
607
+
```{exercise-end}
549
608
```
550
609
551
-
```{solution-start} exercise_3
552
-
:label: solution_3
610
+
```{solution-start} func_ex3
553
611
:class: dropdown
612
+
```
554
613
555
614
Here's a function for the first random device.
556
-
```
615
+
616
+
617
+
557
618
558
619
```{code-cell} python3
559
620
from numpy.random import uniform
@@ -597,3 +658,100 @@ draw_new(3)
597
658
598
659
```{solution-end}
599
660
```
661
+
662
+
663
+
## Advanced Exercises
664
+
665
+
In the following exercises, we will write recursive functions together.
666
+
667
+
We will use more advanced syntaxes such as {any}`list comprehensions <list_comprehensions>` to test our solutions against a list of inputs.
668
+
669
+
If you are not familiar with these concepts, feel free to come back later.
The first few numbers in the sequence are $0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55$.
1664
-
1665
-
Write a function to recursively compute the $t$-th Fibonacci number for any $t$.
1666
-
1667
-
```{exercise-end}
1668
-
```
1669
-
1670
-
```{solution-start} paf_ex1
1671
-
:class: dropdown
1672
-
```
1673
-
1674
-
Here's the standard solution
1675
-
1676
-
```{code-cell} python3
1677
-
def x(t):
1678
-
if t == 0:
1679
-
return 0
1680
-
if t == 1:
1681
-
return 1
1682
-
else:
1683
-
return x(t-1) + x(t-2)
1684
-
```
1685
-
1686
-
Let's test it
1687
-
1688
-
```{code-cell} python3
1689
-
print([x(i) for i in range(10)])
1690
-
```
1691
-
1692
-
```{solution-end}
1693
-
```
1694
-
1695
1602
1696
1603
```{exercise-start}
1697
-
:label: paf_ex2
1604
+
:label: paf_ex1
1698
1605
```
1699
1606
1700
1607
Complete the following code, and test it using [this csv file](https://raw.githubusercontent.com/QuantEcon/lecture-python-programming/master/source/_static/lecture_specific/python_advanced_features/test_table.csv), which we assume that you've put in your current working directory
@@ -1720,7 +1627,7 @@ for date in dates:
1720
1627
```{exercise-end}
1721
1628
```
1722
1629
1723
-
```{solution-start}paf_ex2
1630
+
```{solution-start}paf_ex1
1724
1631
:class: dropdown
1725
1632
```
1726
1633
@@ -1755,7 +1662,7 @@ for date in dates:
1755
1662
1756
1663
1757
1664
```{exercise-start}
1758
-
:label: paf_ex3
1665
+
:label: paf_ex2
1759
1666
```
1760
1667
1761
1668
Suppose we have a text file `numbers.txt` containing the following lines
@@ -1777,7 +1684,7 @@ Using `try` -- `except`, write a program to read in the contents of the file and
0 commit comments