## WeBWorK Problems

### reduced fractions ### reduced fractions

by Kenneth Appel -
Number of replies: 4
I would like a context that does not permit
unreduced fractions. I suspect that there is
none since this is something that seems only important at the k-12 level.

fraction and then use a gcd subroutine (which I
already have functioning). What I do not know
is how to
1. See if the answer given is a fraction
2. grab that fraction to check to see if it is
reduced.
Ken ### Re: reduced fractions

by Davide Cervone -
Ken:

Here's a code snippet that used the LimitedNumeric-StructFraction context to force fraction input, and then uses answerHints as a filter to check if the fraction is reduced.

    loadMacros("answerHints.pl");

Context("LimitedNumeric-StrictFraction");

sub {
my ($correct,$student, $ans) = @_; return 0 if$ans->{isPreview};
my $node =$student->{equation}{tree};
return 0 unless $node->class eq 'DIVIDE'; my ($a,$b) = (abs($node->{lop}->eval),abs($node->{rop}->eval)); my ($ra,$rb) = reduce($a,$b); return$a != $ra ||$b != $rb; } => ["Your answer isn't reduced", checkCorrect => 1, score => 0] )));  This answerHint uses a checking subroutine that is called even when the answer is correct (checkCorrect => 1). It does not issue its message if the Preview button was pressed. Otherwise, it looks up the top node of the parsed equation and checks if it is a DIVIDE node. If not, it returns without issuing its message. If the answer is a division, it looks up the (absolute value of) the numerator and denominator, and reduces them to lowest terms (the reduce() routine is in PGauxiliaryFunction.pl), and checks to see that the student's answer is the same as the reduced one. If not, the message is issued and the score is set to zero in that case (due to score => 0). Of course, one could package up this AnswerHint() call as a value in a macro file, say filterReducedFaction.pl, containing  loadMacros("answerHints.pl"); @reducedFraction = AnswerHints( sub { my ($correct, $student,$ans) = @_;
return 0 if $ans->{isPreview}; my$node = $student->{equation}{tree}; return 0 unless$node->class eq 'DIVIDE';
my ($a,$b) = (abs($node->{lop}->eval),abs($node->{rop}->eval));
my ($ra,$rb) = reduce($a,$b);
return $a !=$ra || $b !=$rb;
} => ["Your answer isn't reduced", checkCorrect => 1, score => 0]
);

and then use
    loadMacros("filterReducedFraction.pl");
Context("LimitedNUmeric-StrictFraction");
ANS(Real("1/2")->cmp->withPostFilter(@reducedFraction))


That should do what you ask, I think.

Davide ### Re: reduced fractions

by Kenneth Appel -
Davide
Thank you very much.

I do not seem to have contextLimitedNumeric-StrictFraction (although I clearly do have contextLimitedNumeric). Where should I get it?
Ken ### Re: reduced fractions

by Davide Cervone -
There is no contextLimitedNumeric-StrictFraction.pl file. The LimitedNumeric-StrictFraction is part of the base MathObjects system and is defined in pg/lib/Parser/Legacy/LimitedNumeric.pl as part of the support for the traditional answer checker's "strict" and "frac" modes.

The upshot is, you should just be able to use Context("LimitedNumeric-StrictFraction") without loading any special macro files.

Davide 