Skip to content

Commit 38e9e9a

Browse files
committed
python_advanced_feature exercise
1 parent 93f75ef commit 38e9e9a

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

lectures/python_advanced_features.md

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,31 @@ Write a function to recursively compute the $t$-th Fibonacci number for any $t$.
16671667
```{exercise-end}
16681668
```
16691669

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+
16701695

16711696
```{exercise-start}
16721697
:label: paf_ex2
@@ -1695,58 +1720,6 @@ for date in dates:
16951720
```{exercise-end}
16961721
```
16971722

1698-
1699-
```{exercise-start}
1700-
:label: paf_ex3
1701-
```
1702-
1703-
Suppose we have a text file `numbers.txt` containing the following lines
1704-
1705-
```{code-block} none
1706-
:class: no-execute
1707-
1708-
prices
1709-
3
1710-
8
1711-
1712-
7
1713-
21
1714-
```
1715-
1716-
Using `try` -- `except`, write a program to read in the contents of the file and sum the numbers, ignoring lines without numbers.
1717-
1718-
```{exercise-end}
1719-
```
1720-
1721-
## Solutions
1722-
1723-
1724-
```{solution-start} paf_ex1
1725-
:class: dropdown
1726-
```
1727-
1728-
Here's the standard solution
1729-
1730-
```{code-cell} python3
1731-
def x(t):
1732-
if t == 0:
1733-
return 0
1734-
if t == 1:
1735-
return 1
1736-
else:
1737-
return x(t-1) + x(t-2)
1738-
```
1739-
1740-
Let's test it
1741-
1742-
```{code-cell} python3
1743-
print([x(i) for i in range(10)])
1744-
```
1745-
1746-
```{solution-end}
1747-
```
1748-
1749-
17501723
```{solution-start} paf_ex2
17511724
:class: dropdown
17521725
```
@@ -1781,6 +1754,29 @@ for date in dates:
17811754

17821755

17831756

1757+
```{exercise-start}
1758+
:label: paf_ex3
1759+
```
1760+
1761+
Suppose we have a text file `numbers.txt` containing the following lines
1762+
1763+
```{code-block} none
1764+
:class: no-execute
1765+
1766+
prices
1767+
3
1768+
8
1769+
1770+
7
1771+
21
1772+
```
1773+
1774+
Using `try` -- `except`, write a program to read in the contents of the file and sum the numbers, ignoring lines without numbers.
1775+
1776+
```{exercise-end}
1777+
```
1778+
1779+
17841780
```{solution-start} paf_ex3
17851781
:class: dropdown
17861782
```

0 commit comments

Comments
 (0)