[LON-CAPA-users] Preprocessor vs. Custom Response

Gerd Kortemeyer kortemey at msu.edu
Fri Dec 4 15:32:10 EST 2015


Hi,

> On Dec 4, 2015, at 2:41 PM, Jacob Bond <bond13 at purdue.edu> wrote:
>> 
>> Are you actually looking for the fraction to be reduced? Because we can check that very generally. 
>> 
> 
> Yes, that is the main idea.

Okay, then I would use Maxima to figure out if the reduced fraction is equal to the fraction input by the student, see below.

- Gerd.

<problem>
<script type="loncapa/perl">
$a=&random(20,100,4);
$b=$a+&random(40,140,4);
$numerical=$a/$b;
$reduced=&cas('maxima',"$a/$b")
</script>

<startouttext />
Input $a/$b as a reduced fraction.
<endouttext />

<customresponse answerdisplay="$reduced">

<answer type="loncapa/perl"># Split nominator and denominator
($nominator,$denominator)=split(/\//,$submission);
# Do we have a nominator? If not, be nice and don't charge a try
unless ($nominator) { return 'BAD_FORMULA'; }
# Is it valid? If not, don't charge a try
$nominator=&cas("maxima",$nominator);
if ($nominator=~/^Error/i) { return 'BAD_FORMULA'; }
# If students just input a number, there would be no denominator
unless ($denominator) {
# Check for exact numerical equivalency:
   if ($nominator==$numerical) { return 'EXACT_ANS'; }
} else {
# There is a denominator
# Again, be nice and don't charge a try for syntax errors
   $denominator=&cas("maxima",$denominator);
   if ($denominator=~/^Error/i) { return 'BAD_FORMULA'; }
# Okay, let's compare the fractions as strings
    if ("$nominator/$denominator" eq $reduced) { return 'EXACT_ANS'; }
}
# Nope, sorry.
return 'INCORRECT';</answer>

    <textline readonly="no" spellcheck="none" />
</customresponse>

</problem>


More information about the LON-CAPA-users mailing list