WeBWorK Problems

Numberless unit answers

Re: Numberless unit answers

by Davide Cervone -
Number of replies: 0
I think this will do what you want:
loadMacros(
  "PGstandard.pl",
  "contextArbitraryString.pl",
  "parserNumberWithUnits.pl",
  "PGcourse.pl",
);

BEGIN_TEXT
The units are: \{ANS_RULE(10)\}
END_TEXT

Context("ArbitraryString");
ANS(Compute("m/s")->cmp(checker => sub {
  my ($correct,$student,$ans) = @_;
  $correct = $correct->value; # get perl string from String object
  $student = $student->value; # ditto
  my $context = Context(); Context("Numeric");
  my $check = NumberWithUnits("1 $correct")->cmp->evaluate("1 $student");
  Context($context);
  $ans->{correct_ans_latex_string} = substr($check->{correct_ans_latex_string},3);
  if ($check->{score}) {
    $ans->{preview_latex_string} = substr($check->{preview_latex_string},3);
    $ans->{preview_text_string} = substr($check->{preview_text_string},3);
  }
  return $check->{score};
}));
This uses the ArbitraryString context so that there are no errors due to unknown units or such, and uses your idea of comparing "1 m/s", which is a good one. Note that you have to change contexts within the cuts checker in order to be able to use NumberWithUnits() (that is a pretty old macro file, and needs some updating). I've also added some code to copy the typeset versions of the student and correct answers so that they will display as typeset fractions and powers.

Hope that does what you need.

Davide