[LON-CAPA-cvs] cvs: modules /gerd/programcheck hartz_example_hints.problem

www www at source.lon-capa.org
Fri Nov 25 12:01:36 EST 2011


www		Fri Nov 25 17:01:36 2011 EDT

  Added files:                 
    /modules/gerd/programcheck	hartz_example_hints.problem 
  Log:
  With hints, modelled after functionplotresponse
  
  

Index: modules/gerd/programcheck/hartz_example_hints.problem
+++ modules/gerd/programcheck/hartz_example_hints.problem
<problem>
<startouttext />
Try testing your solution to the previous problem with arguments
<tt>quadraticRoots(1, 2, 3)</tt>.  You'll get an error because the
roots of the quadratic equation with those coefficients are
complex.  
<p>
Python actually has complex numbers as a primitive data type.  There
are two ways to make a complex number:
<pre>
>>> complex(1, 2)
(1+2j)
>>> 1+2j
(1+2j)
</pre>
If you want to get the parts out of a complex number, you can do the
following:
<pre>
>>> thing = complex(1, 2)
>>> thing.imag
2.0
>>> thing.real
1.0
</pre>
<i>You're probably used to using <i>i</i> for (-1)<sup>0.5</sup>.  Just to
confuse you, we're going to use <i>j</i> instead.  Why?  Because to an
electrical engineer, <i>i</i> stands for current, and there's no arguing.</i>
<p>
Now, write a new procedure <tt>quadraticRootsComplex(a, b, c)</tt>
that computes quadratic roots, as before (including the a=0 case), but works on any real
arguments and returns complex roots if necessary.
<endouttext />

<coderesponse type="pythoncode">
<textfield>
def quadraticRootsComplex(a, b, c):
    #YOUR CODE HERE
    pass
</textfield>

<coderesponseelements>
<answer>
def quadraticRootsComplex(a, b, c):
    if a == 0:
        return -float(c)/float(b)
    a = complex(a, 0)
    b = complex(b, 0)
    c = complex(c, 0)
    det = (b * b - 4 * a * c) ** 0.5
    return [(-b + det) / (2 * a), (-b - det) / (2 * a)]
</answer>

<preanswer>
#any code we would want to run before the student's code goes here
</preanswer>

<postanswer>
#any code to be run after the student's code (but before the test cases) goes here
import random
</postanswer>
</coderesponseelements>

<coderesponseruleset>
<coderesponsecomparisonrule name="aiszero">quadraticRootsComplex(0,random.uniform(3,5), random.uniform(6,7))</coderesponsecomparisonrule>
<coderesponsecomparisonrule name="bisegative">set(quadraticRootsComplex(1,-2,3))</codecomparisonrule>
<coderesponsecomparisonrule name="bcarepositive">set(quadraticRootsComplex(1,2,3))</codecomparisonrule>
</coderesponseruleset>

<hintgroup showoncorrect="no">
<hintpart on="aiszero">
<startouttext />The code fails for a=0.<endouttext />
</hintpart>
<hintpart on="bisnegative">
<startouttext />The code fails if b is negative.<endouttext />
</hintpart>
<hintpart on="bcarepositive">
<startouttext />The code fails if b and c are both positive.<endouttext />
</hintpart>
</hintgroup>

</coderesponse>
</problem>




More information about the LON-CAPA-cvs mailing list