Skip to content

Commit bb47f79

Browse files
committed
ipynb
1 parent f063228 commit bb47f79

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

01-basics.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ <h2>Key Points</h2>
6666
<ul>
6767
<li>Tests compare that the result observed from running code is the same as what was expected ahead of time.</li>
6868
<li>Tests should be written at the same time as the code they are testing is written.</li>
69-
<li>The person best suited to write a test is the author of the original code.</li>
7069
<li>Assertions and exceptions are like alarm systems embedded in the software, guarding against exceptional bahavior.</li>
7170
<li>Unit tests try to test the smallest pieces of code possible, usually functions and methods.</li>
7271
<li>Integration tests make sure that code units work together properly.</li>

01-basics.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ software work together as expected.
5555
>
5656
> - Tests compare that the result observed from running code is the same as what was expected ahead of time.
5757
> - Tests should be written at the same time as the code they are testing is written.
58-
> - The person best suited to write a test is the author of the original code.
5958
> - Assertions and exceptions are like alarm systems embedded in the software, guarding against exceptional bahavior.
6059
> - Unit tests try to test the smallest pieces of code possible, usually functions and methods.
6160
> - Integration tests make sure that code units work together properly.

02-assertions.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,22 @@ <h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2
5252
<span class="kw">assert</span> <span class="dt">len</span>(num_list) != <span class="dv">0</span>
5353
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre>
5454
<p>The advantage of assertions is their ease of use. They are rarely more than one line of code. The disadvantage is that assertions halt execution indiscriminately and the helpfulness of the resulting error message is usually quite limited.</p>
55+
<p>Also, input checking may require decending a rabbit hole of exceptional cases. What happens when the input provided to the mean function is a string, rather than a list of numbers?</p>
5556
<section class="challenge panel panel-success">
5657
<div class="panel-heading">
57-
<h2><span class="glyphicon glyphicon-pencil"></span>Insert an Assertion</h2>
58+
<h2><span class="glyphicon glyphicon-pencil"></span>Challenge Insert an Assertion</h2>
5859
</div>
5960
<div class="panel-body">
6061
<ol style="list-style-type: decimal">
61-
<li>Create a new IPython Notebook</li>
62+
<li>Open an IPython Notebook</li>
6263
<li>Create the following function:</li>
6364
</ol>
6465
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
6566
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre>
6667
<ol start="3" style="list-style-type: decimal">
67-
<li>In the function, insert an assertion that checks whether the list is made of numbers.</li>
68+
<li>In the function, insert an assertion that checks whether the input is actually a list.</li>
6869
</ol>
70+
<p>Hint: Use the <a href="https://docs.python.org/2/library/functions.html#isinstance">isinstance function</a>.</p>
6971
</div>
7072
</section>
7173
<p>Assertions are also helpful for catching abnormal behaviors, such as those that arise with floating point arithmetic.</p>

02-assertions.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
},
114114
"outputs": [],
115115
"source": [
116-
"mean([\"nonempty\",\"but\",\"not\", \"numbers\"])"
116+
"mean(\"nonempty-nonlist\")"
117117
]
118118
},
119119
{
@@ -122,7 +122,7 @@
122122
"source": [
123123
"____\n",
124124
"### Challenge: Insert an Assertion\n",
125-
"In the following code, insert an assertion that checks whether the list is made of numbers."
125+
"In the following code, use the [isinstance function](https://docs.python.org/2/library/functions.html#isinstance) to insert an assertion that checks whether the input is a list."
126126
]
127127
},
128128
{
@@ -136,7 +136,7 @@
136136
"def mean(num_list):\n",
137137
" assert len(num_list) != 0, \"the input is empty\"\n",
138138
" # insert your assertion here\n",
139-
" return sum(num_list)/len(num_list)"
139+
" return sum(num_list)/len(num_list)"
140140
]
141141
},
142142
{
@@ -160,7 +160,7 @@
160160
"outputs": [],
161161
"source": [
162162
"# Does this cause it to halt with a type message?\n",
163-
"mean([\"test\"])"
163+
"mean(\"test\")"
164164
]
165165
},
166166
{
@@ -200,9 +200,9 @@
200200
"outputs": [],
201201
"source": [
202202
"from mynum import a\n",
203-
"# greater than 2 assertion here\n",
204-
"# 2 decimal places assertion here\n",
205-
"# 0.003 assertion here"
203+
"# a is greater than 2 assertion here\n",
204+
"# a is within 2 decimal places of 2 assertion here\n",
205+
"# a is within 0.003 of 2 assertion here"
206206
]
207207
},
208208
{

02-assertions.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,26 @@ line of code. The disadvantage is that assertions halt execution
4444
indiscriminately and the helpfulness of the resulting error message is usually
4545
quite limited.
4646

47+
Also, input checking may require decending a rabbit hole of exceptional cases.
48+
What happens when the input provided to the mean function is a string, rather
49+
than a list of numbers?
4750

48-
> ## Insert an Assertion {.challenge}
51+
52+
> ## Challenge Insert an Assertion {.challenge}
53+
>
4954
>
50-
> 1. Create a new IPython Notebook
55+
> 1. Open an IPython Notebook
5156
> 2. Create the following function:
5257
>
5358
> ~~~ {.python}
5459
> def mean(num_list):
5560
> return sum(num_list)/len(num_list)
5661
> ~~~
5762
>
58-
> 3. In the function, insert an assertion that checks whether the list is
59-
> made of numbers.
63+
> 3. In the function, insert an assertion that checks whether the input is
64+
> actually a list.
65+
>
66+
> Hint: Use the [isinstance function](https://docs.python.org/2/library/functions.html#isinstance).
6067
>
6168
6269

0 commit comments

Comments
 (0)