Skip to content

Commit 87b050a

Browse files
zmx721joelostblom
andauthored
Quarto mod6 (#77)
* remove files not needed for quarto * add basic environment file * quarto file structure and update style * remove sklearn specific style * update quiz background color * update quiz correctAnswers logic * module6 * fix issues caused by pandas * fix pyodide error * update non-executed outputs --------- Co-authored-by: Joel Ostblom <[email protected]>
1 parent bd05f65 commit 87b050a

23 files changed

+4086
-0
lines changed

_quarto.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ website:
182182
- text: '&nbsp;&nbsp; 6.1. Exercises'
183183
href: modules/module5/module5-20-function_questions.qmd
184184
- href: modules/module5/module5-23-what_did_we_just_learn.qmd
185+
- section: "**Module 6: Functions Fundamentals and Best Practices**"
186+
contents:
187+
- href: modules/module6/module6-00-module_learning_outcomes.qmd
188+
- href: modules/module6/module6-01-dry_revisited_and_function_fundamentals.qmd
189+
- text: '&nbsp;&nbsp; 1.1. Exercises'
190+
href: modules/module6/module6-02-questions_on_scoping.qmd
191+
- href: modules/module6/module6-05-default_arguments.qmd
192+
- text: '&nbsp;&nbsp; 2.1. Exercises'
193+
href: modules/module6/module6-06-will_it_output.qmd
194+
- href: modules/module6/module6-09-function_docstrings.qmd
195+
- text: '&nbsp;&nbsp; 3.1. Exercises'
196+
href: modules/module6/module6-10-docstring_questions.qmd
197+
- href: modules/module6/module6-13-defensive_programming_using_exceptions.qmd
198+
- text: '&nbsp;&nbsp; 4.1. Exercises'
199+
href: modules/module6/module6-14-exceptions.qmd
200+
- href: modules/module6/module6-17-unit_tests.qmd
201+
- text: '&nbsp;&nbsp; 5.1. Exercises'
202+
href: modules/module6/module6-18-assert_questions.qmd
203+
- href: modules/module6/module6-22-good_function_design_choices.qmd
204+
- text: '&nbsp;&nbsp; 6.1. Exercises'
205+
href: modules/module6/module6-23-function_design_questions.qmd
206+
- href: modules/module6/module6-26-what_did_we_just_learn.qmd
185207

186208
# Since we are declaring options for two formats here (html and revealjs)
187209
# each qmd file needs to include a yaml block including which format to use for that file.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 0. Module Learning Outcomes
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/UkRmtvYNylA?start=0&end=36&rel=0"
16+
title="Module 6 Video - Module Learning Outcomes"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module6_00.html"
27+
></iframe>
28+
29+
:::
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 1. DRY Revisited and Function Fundamentals
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/yz6Wwa2MkQA?start=2008&end=3122&rel=0"
16+
title="Module 6 Video - DRY Revisited and Function Fundamentals"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module6_01.html"
27+
></iframe>
28+
29+
:::
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
---
2+
format: live-html
3+
---
4+
5+
<script src='../../src/quiz.js'></script>
6+
7+
# 1.1. Exercises
8+
9+
## Questions on Scoping
10+
11+
```python
12+
toy = "ball"
13+
14+
def playtime():
15+
toy = "truck"
16+
return toy
17+
18+
toy
19+
```
20+
21+
<div id='mcq1'></div>
22+
<script>
23+
generateQuiz(
24+
'mcq1',
25+
'Question 1',
26+
'Given the code above, what is the output?',
27+
{
28+
'<code>ball</code>': 'Well done!',
29+
'<code>truck</code>': 'Since <code>toy</code> is being called in the global environment, the output of <code>toy</code> is the value of the global variable value of <code>toy</code>.',
30+
'<code>Error</code>': 'This will still run without an error!',
31+
},
32+
'<code>ball</code>',
33+
);
34+
</script>
35+
36+
```python
37+
toy = "ball"
38+
39+
def playtime():
40+
toy = "truck"
41+
return toy
42+
43+
playtime()
44+
```
45+
46+
<div id='mcq2'></div>
47+
<script>
48+
generateQuiz(
49+
'mcq2',
50+
'Question 2',
51+
'Given the code above, what is the output?',
52+
{
53+
'<code>ball</code>': '<code>toy</code> is equal to <code>"truck"</code> in the local environment so it will return the local variable value of <code>toy</code>.',
54+
'<code>truck</code>': '',
55+
'<code>Error</code>': 'This will still run without an error!',
56+
},
57+
'<code>truck</code>',
58+
);
59+
</script>
60+
61+
```python
62+
def playtime():
63+
toy = "truck"
64+
return toy
65+
66+
toy
67+
```
68+
69+
<div id='mcq3'></div>
70+
<script>
71+
generateQuiz(
72+
'mcq3',
73+
'Question 3',
74+
'Given the code above, what is the output?',
75+
{
76+
'<code>None</code>': '<code>toy</code> is a local variable, will it be recognized in a global environment?',
77+
'<code>truck</code>': '<code>toy</code> is a local variable, will it be recognized in a global environment?',
78+
'<code>Error</code>': 'Great! <code>toy</code> is a local variable, and will not be recognized in the global environment.',
79+
},
80+
'<code>Error</code>',
81+
);
82+
</script>
83+
84+
85+
## Side Effects
86+
87+
<div id='mcq4'></div>
88+
<script>
89+
generateQuiz(
90+
'mcq4',
91+
'True or False',
92+
'Side effects make your code easier to debug.',
93+
{
94+
'True': 'Ahh! Please go back and read over the slides. Side effects are generally avoided.',
95+
'False': 'Well done. This is an important one to get right!',
96+
},
97+
'False',
98+
);
99+
</script>
100+
101+
A.
102+
103+
```python
104+
toy = "ball"
105+
106+
def playtime():
107+
toy = "truck"
108+
print(toy)
109+
110+
playtime()
111+
```
112+
113+
B.
114+
115+
```python
116+
117+
toy = "ball"
118+
119+
def playtime():
120+
toy = "truck"
121+
return toy
122+
123+
playtime()
124+
```
125+
126+
<div id='mcq5'></div>
127+
<script>
128+
generateQuiz(
129+
'mcq5',
130+
'Question 2',
131+
'Which of the above functions produce a side effect?',
132+
{
133+
'A': 'Nice job! Printing <code>toy</code> is considered a function side effect!',
134+
'B': 'Having a function <code>return</code> something is not considered necessarily a side effect.',
135+
'A and B': 'Having a function <code>return</code> something is not considered necessarily a side effect.',
136+
'Neither A or B': 'Printing <code>toy</code> is considered a function side effect!',
137+
},
138+
'A',
139+
);
140+
</script>
141+
142+
<div id='mcq6'></div>
143+
<script>
144+
generateQuiz(
145+
'mcq6',
146+
'True or False',
147+
'If a function modifies a variable in the function’s local environment, that is considered a side effect.',
148+
{
149+
'True': 'This is not considered a side effect if a variable created within a function is modified within the same function.',
150+
'False': '',
151+
},
152+
'False',
153+
);
154+
</script>
155+
156+
157+
## Writing Functions Without Side Effects
158+
159+
**Instructions:**
160+
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.
161+
162+
**When you see `____` in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.**
163+
164+
_**Make sure you remove the hash (`#`) symbol in the coding portions of this question. We have commented them so that the line won't execute and you can test your code after each step.**_
165+
166+
The function `kg_to_lb()` is used to convert a list of elements with kg units into a list of elements with lbs units. Unfortunate this function includes a side effect that edits one of the global variables.
167+
168+
```{pyodide}
169+
weight_kg = [90, 41, 65, 76, 54, 88]
170+
weight_lb = list()
171+
172+
def kg_to_lb(weight_list):
173+
conversion = 2.20462
174+
for kg in weight_kg:
175+
weight_lb.append(kg * conversion)
176+
return
177+
178+
kg_to_lb(weight_kg)
179+
weight_lb
180+
```
181+
182+
**Tasks:**
183+
184+
- Write a new function named `better_kg_to_lb` that no longer contains a side effect.
185+
- Test your new function on the `weight_kg` list and save the results in an object named `weight_lb_again`.
186+
187+
```{pyodide}
188+
#| setup: true
189+
#| exercise: writing_functions_without_side_effects
190+
import pandas as pd
191+
```
192+
193+
```{pyodide}
194+
#| exercise: writing_functions_without_side_effects
195+
weight_kg = [90, 41, 65, 76, 54, 88]
196+
197+
# rewrite the code and function above so that it does not have any side effects
198+
____ ____(____):
199+
____
200+
____
201+
____
202+
____
203+
204+
# Test your new function on weight_kg and name the new object weight_lb_again
205+
____ = ____
206+
____
207+
```
208+
209+
```{pyodide}
210+
#| exercise: writing_functions_without_side_effects
211+
#| check: true
212+
from src.utils import print_correct_msg
213+
214+
assert isinstance(result, list), "Your function should return a list."
215+
assert len(result) == 6, "Your function output is incorrect. Are you getting rid of side effects?"
216+
assert round(sum(result), 2) == 912.71, "Your function output is incorrect. Check your calculation."
217+
print_correct_msg()
218+
```
219+
220+
:::: { .hint exercise="writing_functions_without_side_effects"}
221+
::: { .callout-note collapse="false"}
222+
223+
## Hint 1
224+
225+
- Are you putting `weight_lb` inside the function now?
226+
- Are you returning `weight_lb`?
227+
228+
:::
229+
::::
230+
231+
:::: { .solution exercise="writing_functions_without_side_effects" }
232+
::: { .callout-tip collapse="false"}
233+
234+
## Fully worked solution:
235+
236+
```{pyodide}
237+
weight_kg = [90, 41, 65, 76, 54, 88]
238+
239+
# rewrite the code and function above so that it does not have any side effects
240+
def better_kg_to_lb(weight_list):
241+
weight_lb = list()
242+
conversion = 2.20462
243+
244+
for kg in weight_list:
245+
weight_lb.append(kg * conversion)
246+
return weight_lb
247+
248+
# Test your new function on weight_kg and name the new object weight_lb_again
249+
weight_lb_again = better_kg_to_lb(weight_kg)
250+
weight_lb_again
251+
```
252+
253+
:::
254+
::::
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 2. Default Arguments
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/yz6Wwa2MkQA?start=3128&end=3481&rel=0"
16+
title="Module 6 Video - Default Arguments"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module6_05.html"
27+
></iframe>
28+
29+
:::

0 commit comments

Comments
 (0)