-
Victor Shnayder authored
- I added them when playing with input types, but never merged them in
23fa860f
codeinput_demo.xml 7.27 KiB
<?xml version="1.0"?>
<problem display_name="L4 Problem 1">
<text>
<p>
<b class="bfseries">Part 1: Function Types</b>
</p>
<p>
For each of the following functions, specify the type of its <b class="bfseries">output</b>. You can assume each function is called with an appropriate argument, as specified by its docstring. </p>
<p>
If the output can be either an int or a float, select num, which isn't a real Python type, but which we'll use to indicate that either basic numeric type is legal. </p>
<p>
In fact, in Python, booleans True and False can be operated on as if they were the integers 1 and 0; but it is ugly and confusing to take advantage of this fact, and we will resolutely pretend that it isn't true. </p> <!-- so why even mention it? -->
<p>
<section class="hints">
<div class="collapsible">
<header>
<a href="#" id="id41">What are those lines under the function definitions?</a>
</header>
<section id="id41">
<p>
In this and future problems, you'll see function definitions that look like this: </p>
<pre>
def a(x):
'''
x: int or float.
'''
return x + 1
</pre>
<p>
What are those three lines between <code>def a(x):</code> and <code>return x + 1</code>? These lines are called the <i class="it">docstring</i> of the function. A docstring is a special type of comment that is used to document what your function is doing. Typically, docstrings will explain what the function expects the type(s) of the argument(s) to be, and what the function is returning. </p>
<p>
In Python, docstrings appear immediately after the <code>def</code> line of a function, before the body. Docstrings start and end with triple quotes - this can be triple single quotes or triple double quotes, it doesn't matter as long as they match. To sum up this general form: </p>
<pre>
def myFunction(argument):
"""
Docstring goes here. Explain what type argument(s) should have, and what your function
is going to return.
"""
< Code for your function (the body of the function) goes here >
</pre>
<p>
As you begin coding your own functions, we strongly encourage you to document all your functions by using properly-formatted docstrings! </p>
</section>
</div>
</section>
</p>
<ol class="enumerate">
<li>
<pre>
def a(x):
'''
x: int or float.
'''
return x + 1
</pre>
<p>
Indicate the type of the output that the function <code>a</code> will yield. <optionresponse><optioninput options="('NoneType','num','int','float','boolean')" correct="num"/></optionresponse> </p>
</li>
<li>
<pre>
def b(x):
'''
x: int or float.
'''
return x + 1.0
</pre>
<p>
Indicate the type of the output that the function <code>b</code> will yield. <optionresponse><optioninput options="('NoneType','num','int','float','boolean')" correct="float"/></optionresponse> </p>
</li>
<li>
<pre>
def c(x, y):
'''
x: int or float.
y: int or float.
'''
return x + y
</pre>
<p>
Indicate the type of the output that the function <code>c</code> will yield. <optionresponse><optioninput options="('NoneType','num','int','float','boolean')" correct="num"/></optionresponse> </p>
</li>
<li>
<pre>
def d(x, y):
'''
x: Can be of any type.
y: Can be of any type.
'''
return x > y
</pre>
<p>
Indicate the type of the output that the function <code>d</code> will yield. <optionresponse><optioninput options="('NoneType','num','int','float','boolean')" correct="boolean"/></optionresponse> </p>
</li>
<li>
<pre>
def e(x, y, z):
'''
x: Can be of any type.
y: Can be of any type.
z: Can be of any type.
'''
return x >= y and x <= z
</pre>
<p>
Indicate the type of the output that the function <code>e</code> will yield. <optionresponse><optioninput options="('NoneType','num','int','float','boolean')" correct="boolean"/></optionresponse> </p>
</li>
<li>
<pre>
def f(x, y):
'''
x: int or float.
y: int or float
'''
x + y - 2
</pre>
<p>
Indicate the type of the output that the function <code>f</code> will yield. <optionresponse><optioninput options="('NoneType','num','int','float','boolean')" correct="NoneType"/></optionresponse> </p>
</li>
</ol>
<p>
<b class="bfseries">Part 2: Transcript</b>
</p>
<p>
Below is a transcript of a session with the Python shell. Assume the functions from Part 1 (above) have been defined. Provide the type and value of the expressions being evaluated. If evaluating an expression would cause an error, select NoneType and write 'error' in the box. If the value of an expression is a function, select function as the type and write 'function' in the box. </p>
<ol class="enumerate">
<li>
<p>
<code>a(6)</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="int"/>
</optionresponse>
<stringresponse answer="7">
<textline size="50"/>
</stringresponse>
</p>
</li>
<li>
<p>
<code>a(-5.3)</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="float"/>
</optionresponse>
<stringresponse answer="-4.3">
<textline size="50"/>
</stringresponse>
</p>
</li>
<li>
<p>
<code>a(a(a(6)))</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="int"/>
</optionresponse>
<stringresponse answer="9">
<textline size="50"/>
</stringresponse>
</p>
</li>
<li>
<p>
<code>c(a(1), b(1))</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="float"/>
</optionresponse>
<stringresponse answer="4.0">
<textline size="50"/>
</stringresponse>
</p>
</li>
<li>
<p>
<code>d('apple', 11.1)</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="boolean"/>
</optionresponse>
<stringresponse answer="True">
<textline size="50"/>
</stringresponse>
</p>
</li>
<li>
<p>
<code>e(a(3), b(4), c(3, 4))</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="boolean"/>
</optionresponse>
<stringresponse answer="False">
<textline size="50"/>
</stringresponse>
</p>
</li>
<li>
<p>
<code>f</code>
<optionresponse>
<optioninput options="('NoneType','num','int','float','boolean','function')" correct="function"/>
</optionresponse>
<stringresponse answer="function">
<textline size="50"/>
</stringresponse>
</p>
</li>
</ol>
</text>
</problem>