A simple multianswer problem.
Download file: Multianswer.pg
DOCUMENT(); loadMacros('PGstandard.pl', 'PGML.pl', 'parserMultiAnswer.pl', 'PGcourse.pl');
Preamble
Since we are using the Multianswer technique, parserMultianswer.pl
must be loaded.
$fac1 = Formula("(1 - x)"); $fac2 = Formula("(1 + x)"); $multians = MultiAnswer($fac1, $fac2)->with( singleResult => 0, checker => sub { my ($correct, $student, $self) = @_; my ($f1stu, $f2stu) = @{$student}; my ($f1, $f2) = @{$correct}; if (($f1 == $f1stu && $f2 == $f2stu) || ($f1 == $f2stu && $f2 == $f1stu)) { return [ 1, 1 ]; } else { if ($f1 == $f1stu || $f2 == $f1stu) { return [ 1, 0 ]; } elsif ($f1 == $f2stu || $f2 == $f2stu) { return [ 0, 1 ]; } else { return [ 0, 0 ]; } } } ); BEGIN_PGML Factor: [`1-x^2 = \big(`] [___]{$multians} [`\big)\big(`] [___]{$multians} [`\big)`] END_PGML
Setup
This problem is shown as an example of using parserMultiAnswer.pl
. A better solution for this type of problem is shown in Factored Polynomial.
In the setup section of the file we define a MultiAnswer
object that knows how to deal with the problem. Here we define an object that will take two answers and check that they are correct (in either order).
First, the singleResult=>0
line indicates that the different answers in the problem will be evaluated as separate answers, rather than as a single unit. Other useful flags include allowBlankAnswers
, checkTypes
, separator
and tex_separator
. These are noted below.
Then, the checker=> section
defines a subroutine to evaluate the problem. It will always have as input a reference to an array of correct answers, a reference to an array of student answers, and a reference to the object itself. (There is a fourth input, too, an answer hash, but we don’t need that here.)
The checker routine then returns a reference to a list of results for the problem. In this case there are two answer blanks, so there are two return values. All return values should be 0
or 1
, according to whether the answer for that answer blank is correct or not. Note that if we made this an “all or nothing” problem (that is, we set singleResult=>1
), then there is only one return value needed, so that we could just return 0
or return 1
.
It is possible to set an answer message that will be displayed when the problem is checked, too. For example, if we wanted to set a message when one of the parts was wrong, we could replace the section of the checker code that deals with incorrect answers with:
if ($f1 == $f1stu || $f2 == $f1stu) {
$self->setMessage(1,"This is correct.");
$self->setMessage(2,"Check your answer " .
"by using FOIL.");
return [1,0];
elsif ($f1 == $f1stu || $f2 == $f2stu) {
} $self->setMessage(1,"Check your answer " .
"by using FOIL.");
$self->setMessage(2,"This is correct.");
return [0,1];
else {
} return [0,0];
}
BEGIN_PGML_SOLUTION Solution explanation goes here. END_PGML_SOLUTION ENDDOCUMENT();
Solution
A solution should be provided here.