## WeBWorK Problems

### Requiring reduced radical in list of assignments

by Chris Hughes -
Number of replies: 2
I'm working on a problem that has this structure
Solve the quadratic equation
 x^2 = 28
And I'd like the students to answer with
 x=2sqrt(7), x=-2sqrt(7)
It uses a custom context LimitedRadical in the attached file. The code below works almost exactly how I'd like it to, but it also allows students to enter
x = sqrt(28),x=-sqrt(28)
In such a situation, I would like a reduction message, similar to the fraction context or indeed to the message provided by this context, to be shown.


DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"parserAssignment.pl",
"PGML.pl",
 "contextLimitedRadical.pl",
 );
##############################################
Context("LimitedRadical"); parser::Assignment->Allow; Context()->operators->redefine(',',using=>',',from=>'Numeric'); Context()->operators->redefine('or',using=>',',from=>'Numeric'); Context()->operators->set( ','=>{string=>' or ',TeX=>'\hbox{ or }'}, 'or'=>{string=>' or ',TeX=>'\hbox{ or }'} );
 Context()->lists->set(List => {separator => " or "}); $var = "x"; Context()->variables->are($var=>'Real'); #$a = random(2,10,1);$a = 7; $ans = Compute("$var=2 sqrt($a),$var=-2sqrt($a)"); ############################################## TEXT(beginproblem()); BEGIN_PGML Solve the quadratic equation  [ [$var]^2 = [$a*4] ].  [_______________________] END_PGML ############################################## # works, but no reduction$showPartialCorrectAnswers = 1; ANS($ans->cmp(entry_type => "solution", extra => sub { my ($student,$ansHash,$nth,$value) = @_; if ($student->type ne "Assignment" && $ansHash->{student_formula}->type ne "Assignment") {$student->context->setError("Your$nth$value should be written $var = ___","",undef,undef,$Value::CMP_WARNING); return; } return Value::Real->typeMatch($student); })->withPostFilter(AnswerHints( ["$var=2sqrt($a)","$var=-2sqrt($a)"] => "Are you sure you have all the solutions?", ["2sqrt($a)","-2sqrt($a)"] => ["Your solution is a correct one, but you should write$var = ___<br>Are you sure you have all the solutions?",replaceMessage=>1], ["2sqrt($a),-2sqrt($a)","-2sqrt($a),2sqrt($a)"] => ["Your solutions are correct, but you should write $var = ___",replaceMessage=>1], ))); ############################################## BEGIN_PGML_SOLUTION [$ans]
END_PGML_SOLUTION
##############################################
ENDDOCUMENT();

I was hoping that I would be able to adapt Davide's solution to this question http://webwork.maa.org/moodle/mod/forum/discuss.php?d=3037 - of my many attempts, this was my most promising start
$showPartialCorrectAnswers = 1; ANS($ans->cmp(entry_type => "a solution",
checker => sub {
my ($correct,$student,$ansHash,$nth,$value) = @_; return 0 if$ansHash->{isPreview} || $correct !=$student;
#$student =$ansHash->{student_formula};
$correct =$correct->{original_formula} if defined $correct->{original_formula}; Context()->flags->set(setSqrt => 1, checkAddSub => 1); delete$correct->{test_values}, $student->{test_values}; my$OK = ($correct ==$student); # check if equal when sqrt's are replaced by 1
Context()->flags->set(setSqrt => 0, checkAddSub => 0);
$check = ($correct==$student); my %ha= %{$ansHash};
for my $key (keys %ha) {push(@answerHashKeys,$key); push(@answerHashKeys,'=>'); push(@answerHashKeys,$ha{$key}); push(@answerHashKeys,$BR);}; Value->Error("$dum $BR check:$check $BR correct is$correct $BR student is$student $BR answerHashKeys is:$BR @answerHashKeys");
$correct->context->setError("You must simplify your answer further") unless$OK;
return $OK; }));  but it lead nowhere- it seems that$student doesn't look the way I need it to.

If anyone could offer any help, I'd really appreciate it. I'd also be interested in learning how the debugging was done- the above technique of outputting the answer hash to the message window was given to me by a colleague; I wonder if there are other techniques to accompany it.

### Re: Requiring reduced radical in list of assignments

by Chris Hughes -
Sorry about the formatting- here's a better version

DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserAssignment.pl",
"PGML.pl",
);
##############################################
Context("LimitedRadical");
parser::Assignment->Allow;
Context()->operators->redefine(',',using=>',',from=>'Numeric');
Context()->operators->redefine('or',using=>',',from=>'Numeric');
Context()->operators->set(
','=>{string=>' or ',TeX=>'\hbox{ or }'},
'or'=>{string=>' or ',TeX=>'\hbox{ or }'}
);
Context()->lists->set(List => {separator => " or "});
$var = "x"; Context()->variables->are($var=>'Real');
#$a = random(2,10,1);$a = 7;
$ans = Compute("$var=2 sqrt($a),$var=-2sqrt($a)"); ############################################## TEXT(beginproblem()); BEGIN_PGML Solve the quadratic equation  [ [$var]^2 = [$a*4] ].  [_______________________] END_PGML ############################################## # works, but no reduction $showPartialCorrectAnswers = 1;
 ANS($ans->cmp(entry_type => "solution", extra => sub { my ($student,$ansHash,$nth,$value) = @_; if ($student->type ne "Assignment" && $ansHash->{student_formula}->type ne "Assignment") {$student->context->setError("Your$nth$value should be written $var = ___","",undef,undef,$Value::CMP_WARNING);
return;
}
return Value::Real->typeMatch($student); })->withPostFilter(AnswerHints( ["$var=2sqrt($a)","$var=-2sqrt($a)"] => "Are you sure you have all the solutions?", ["2sqrt($a)","-2sqrt($a)"] => ["Your solution is a correct one, but you should write$var = ___<br>Are you sure you have all the solutions?",replaceMessage=>1],
["2sqrt($a),-2sqrt($a)","-2sqrt($a),2sqrt($a)"] => ["Your solutions are correct, but you should write $var = ___",replaceMessage=>1], ))); ############################################## BEGIN_PGML_SOLUTION [$ans]
END_PGML_SOLUTION
##############################################
ENDDOCUMENT();


### Re: Requiring reduced radical in list of assignments

by Alex Jordan -
We believe we have a working template for this now, in case anyone follows the thread.

The attached problem asks for solutions to x^2=28 (or something similar) and expects variants of "x=2sqrt(7) or x =-2sqrt(7)". Tailored feedback is given for unreduced decimals, and several other things.