-
Notifications
You must be signed in to change notification settings - Fork 0
/
3MysteryFunction.html
52 lines (46 loc) · 4.35 KB
/
3MysteryFunction.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<h3 id="problem-in-s18-211211-mystery-mathstrings">Problem in S18: 21.1/21.1) Mystery Math/Strings</h3>
<h2 id="learning-objectives">Learning Objectives:</h2>
<ul>
<li>Reading and responding to unit tests</li>
<li>Explain how functions are tested for correctness </li>
<li>Interpret a failing unit test</li>
</ul>
<h2 id="problem-the-function-below-fails-its-unit-test-it-is-supposed-to-consume-two-numbers-do-something-then-return-the-result-determine-what-the-function-is-supposed-to-do-then-fix-it-so-it-passes-unit-tests">Problem: The function below fails its unit test. It is supposed to consume two numbers, do something, then return the result. Determine what the function is supposed to do then fix it so it passes unit tests.</h2>
<pre class="codehilite"><code>def mystery_machine(a, b):
return 0
print(mystery_machine(1,2))</code></pre>
<h3 id="step-0-understanding-the-problem">Step 0: Understanding the problem</h3>
<p>In this case we have no idea what the function is supposed to do, so we have to figure it out. While this may seem daunting, understanding how unit tests work can help us out. </p>
<h3 id="step-1-how-do-unit-tests-work">Step 1: How do unit tests work?</h3>
<p>When you click run, Blockpy does a lot more than simply run your code and check the output for correctness. If it only checked your output, you could pass every programming problem by simply printing the expected output instead of doing the problem. Behind the scenes, Blockpy runs the code you wrote and then also runs unit tests. These unit tests run the function you wrote a bunch of times with all kinds of different inputs and compares the result to the expected output. As soon as one of these tests fail, the unit tests stop and gives you the “Incorrect Answer” message. If all the unit tests pass, you get the “Correct Answer” message. We can use this knowledge to figure out what the expected output of our mystery function is by running the unit tests.</p>
<h3 id="step-2-run-the-unit-tests">Step 2: Run the unit tests.</h3>
<p>To determine the expected output, we can run the unit tests to see what the expected output is for a given input. Here’s the result </p>
<p>Feedback: Incorrect Answer <br />
Instructor Feedback<br />
Your mystery_machine function did not return the right value.<br />
Given arguments: <code>2, 2</code><br />
Expected return: <code>4</code><br />
Actually returned: <code>0</code></p>
<h3 id="step-3-determining-the-functionality">Step 3: Determining the functionality:</h3>
<p>So when we give our function the numbers 2 and 2, it should return 4. We know 2*2 is 4 so maybe our function is simply supposed to multiply the two numbers and return the result. </p>
<h3 id="step-4-trying-our-guess">Step 4: Trying our guess</h3>
<p>Now that we think we know what mystery_machine should do, lets run the unit tests again. After modifying the function.</p>
<pre class="codehilite"><code>def mystery_machine(a,b):
return a * b</code></pre>
<p>After running the unit tests again, this is our output:</p>
<p>Feedback: Incorrect Answer <br />
Instructor Feedback<br />
Your mystery_machine function did not return the right value.<br />
Given arguments: <code>4, 3</code><br />
Expected return: <code>7</code><br />
Actually returned: <code>12</code></p>
<p>Looks like our function pass the first unit test (input of 2 and 2) but failed the test when it was given 4 and 3. The expected output was 7 but our function produced 12. Based on this we realize <code>mystery_machine</code> is actually supposed to add the two numbers, not multiply them. This will pass the first unit test because 2+2 produces the expected 4, and the second because 4+3=7. </p>
<h3 id="step-4-trying-again">Step 4: Trying again:</h3>
<p>Let’s modify the function again to return the sum of the two numbers instead of the product and run the unit tests again. </p>
<pre class="codehilite"><code>def mystery_machine(a,b):
return a+b</code></pre>
<p>This time the unit test gives us this output:</p>
<p>Feedback: Complete <br />
Complete!<br />
Great work!</p>
<p>That’s it! Just based on the unit test results we were able to figure out what the function is supposed to do. When we already have an understanding of what a function is supposed to do and we think it should work, unit tests can help us figure out situations where our function produces the wrong output for certain edge cases. </p>