WeBWorK Main Forum

give reduced fraction warning even if answer is a string

give reduced fraction warning even if answer is a string

by Rick Lynch -
Number of replies: 3

I am working on a simple slope problem where one part is a reduced fraction and the other is DNE. I do not want to "give away" which one is DNE, but as of right now, the first part that has a fraction as the answer gives a "Your fraction is not reduced" warning, while the second does not. While not many students will realize this gives away that the second answer is DNE, I'd rather fix it as it bugs me, hah! In the picture below, I'd like the second blank to also throw the same warning. I tried to use $m2->cmp(typeMatch => Fraction(1)) but this seems to not work. My full code is attached.


In reply to Rick Lynch

Re: give reduced fraction warning even if answer is a string

by Danny Glin -
Try changing String("DNE") to Compute("DNE"). I believe that using String forces the $m2 object to be created in the String context, which doesn't know anything about fractions. Compute will try to use the default context for the problem, which in this case is LimitedFraction. I believe that the LimitedFraction context should already contain the string "DNE", since it is included in the Numeric context. If not, you can add it.
In reply to Danny Glin

Re: give reduced fraction warning even if answer is a string

by Rick Lynch -
Thanks for the input. Unfortunately, this didn't work and I got the same behavior.
In reply to Rick Lynch

Re: give reduced fraction warning even if answer is a string

by Davide Cervone -

There are several issues involved, here.

First, the reduced fraction warning is produces by a post-processor on the Fraction answer checker, so it is not called for when the string checker is being used. You would need to run the post-processor by hand to get it to process a Fraction answer when the correct answer is a String.

Second there are answer checker flags that control whether students must reduce fractions, and whether an error is displayed telling them that if they don't do so (studentsMustReduceFractions and showFractionReduceWarnings). But these are only applied to Fraction objects, so you would need to set them for the String answer that you are using, either in the cmp() call, or in the Context's cmpDefaults list.

Third, there is a flag that controls whether fractions are reduced automatically (reduceFractions), and it is true by default, (but the Fraction contexts set it to the negation of studentsMustReduceFractions), so you would need to set this yourself in the Context in order to make it have an effect during processing of the student's answer when the correct one is a string.

So one possible solution is the following:

Context("LimitedFraction");
Context()->flags->set(reduceFractions => 0);
Context()->{cmpDefaults}{String}{studentsMustReduceFractions} = 1;
Context()->{cmpDefaults}{String}{showFractionReduceWarnings} = 1;

$ans = Compute("DNE");

BEGIN_TEXT
Answer is "DNE": \{and_rule(10)\}
END_TEXT

ANS($ans->cmp()->withPostFilter(sub {
  my $ans = shift; 
  context::Fraction::Fraction::cmp_postprocess($ans->{correct_value}, $ans);
  return $ans;
}));

If you need this a lot, you could package up the context settings and the post-filter into a macro file that you load.

An alternative would be to subclass the String object and have it do this stuff automatically, but it is probably not worth it unless you need to do this a lot. If so, I can work that out for you.