WeBWorK Problems

Custom checker: Incorrect answers marked as correct

Custom checker: Incorrect answers marked as correct

by Tim Alderson -
Number of replies: 2
Hi,

The following code marks incorrect answers as correct. I can't seem to spot the error(s). Any help would be most appreciated.

Thanks!

Tim.


## DESCRIPTION
##
## ENDDESCRIPTION


## DBsubject(Linear Algebra)
## DBchapter(Determinants)
## DBsection(Properties)
## Date(2018-05-09)
## Institution(University of Lethbridge)
## Author(Mitchell Sulz-Martin)
## MO(1)
## KEYWORDS('ULETH-MATH1410', 'matrix', 'determinant')


##################
# Initialization

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
# Used to provide contextual help for how to type answers.
"AnswerFormatHelp.pl",
# Provides greater control over the layout of the problem.
"PGML.pl",
# Used for course-specific initializations.
"PGcourse.pl",
);

TEXT(beginproblem());


#############################
# Setup

# Used for handling matrix problems.
Context("Matrix");

#-ULETH-#
# ans : the random value of the determinant for the question.
# M : A solution matrix used to verify a student answer or act as a solution set.

$ans = non_zero_random(-10,10,1);
$M = Matrix([
[$ans,6,1,9],
[0,1,3,4],
[0,0,1,6],
[0,0,0,1],
]);
#-ENDULETH-#



#############################
# Main text

#-ULETH-#

BEGIN_PGML
Enter a non-diagonal matrix [` A `], with [`det(A)=`] [$ans].

[`A =`] [@ $M->ans_array(5) @]* [@ AnswerFormatHelp("matrices") @]*

END_PGML
#-ENDULETH-#


#-ULETH-#
$showPartialCorrectAnswers = 0;
ANS( $M->cmp(
checker => sub {
my ($M,$student,$ansHash) = @_;
my ($sdet)=$student->det();
return ($sdet != $ans or $student->is_symmetric ? 0 : 1);
}
));
#-ENDULETH-#


#############################
# Solution

#-ULETH-#

BEGIN_PGML_SOLUTION
SOLUTION:

One possible solution is [`A = [$M]`].

We know that the determinant of any upper triangular matrix is the product of the element along the diagonal. So by placing [`[$ans]`] anywhere on the diagonal and filling the upper half of the matrix with any numbers (since they do not effect the determinant).

END_PGML_SOLUTION

COMMENT('
Randomization provides 19 different possible versions of this question.<BR>
Includes a solution set.<BR>
Recommended Settings:<BR>
- Weight: 2<BR>
- Max attempts: Unlimited<BR>
- Show me another: -2<BR>
- Rerandomize after: Default<BR>
Made from a ULETH template.<BR>
');
#-ENDULETH-#

ENDDOCUMENT();

In reply to Tim Alderson

Re: Custom checker: Incorrect answers marked as correct

by Davide Cervone -
I believe the problem is that the "or" operator in perl has lower precedence than "... ? ... : ...", and so
return ($sdet != $ans or $student->is_symmetric ? 0 : 1);
is the same as
return ($sdet != $ans or ($student->is_symmetric ? 0 : 1));
where as I think you want
return (($sdet != $ans or $student->is_symmetric) ? 0 : 1);
So, for example, with your code, a non-symmetric matrix will be marked correct, not matter what, as will any matrix with determinant that isn't correct.

Note also that the || operator has higher precedence than ... ? ... : ..., so

return ($sdet != $ans || $student->is_symmetric ? 0 : 1);
would also work.

Finally, you might want to use [`\det(A)=[$ans]`] rather than [`det(A)=`] [$ans] in order to format the "det" in the proper font, and to get the answer value to be part of the typeset expression so that negatives are properly handled (and also making it easier for students with screen readers).

In reply to Davide Cervone

Re: Custom checker: Incorrect answers marked as correct

by Tim Alderson -
Thank you very much for the detailed and most helpful reply Davide.

All works well now.