Forum archive 2000-2006

Zbigniew Fiedorowicz - WeBWorK programming question

Zbigniew Fiedorowicz - WeBWorK programming question

by Arnold Pizer -
Number of replies: 0
inactiveTopicWeBWorK programming question topic started 9/17/2003; 9:14:25 PM
last post 9/17/2003; 9:14:25 PM
userZbigniew Fiedorowicz - WeBWorK programming question  blueArrow
9/17/2003; 9:14:25 PM (reads: 576, responses: 0)
I was writing an algebra simplification problem for a colleague, which asks a student to simplify an expression involving the difference of two rational functions.  The challenge here is to make sure that the student is really simplifying, not just reentering the original difference expression, perhaps by cloaking it with negative exponents.  So the idea is to peek at the student's answer before passing it on to function_cmp(), to rule out multiple /'s, ^'s and **'s. Here's what I finally came up with:
<pre>
##DESCRIPTION
##  Algebra problem: Solving linear equations
##  Authored by Rose Hart and Zig Fiedorowicz
##  September, 2003
##ENDDESCRIPTION
##KEYWORDS('algebra', 'rational functions')
DOCUMENT();        # This should be the first executable line in the problem.
loadMacros(
PG.pl,
PGbasicmacros.pl,
PGchoicemacros.pl,
PGanswermacros.pl,
PGauxiliaryFunctions.pl
);
TEXT(&beginproblem);
$a =  random(1,5,1);
$b =  random(1,5,1);
TEXT(EV2(<<EOT));
Subtract:
[ frac{$a x}{x- $b} - frac5{$b -x} ]
$BR
Answer: {NAMED_ANS_RULE(my_answer,35)}
EOT
$ans = "($a*x+5)/(x- $b)";
#First make sure that there are no multiple fractions in the expression
$Answer = $inputs_ref->{my_answer};
$Answer = '' unless defined($Answer);
@chars = split //, $Answer;
$numSlashes = 0;
foreach $char (@chars) {
  if ($char eq '/') {$numSlashes++;}
}
if ($numSlashes > 1)
     {NAMED_ANS(my_answer,strict_str_cmp($ans));}
else
     {NAMED_ANS(my_answer,function_cmp($ans));}
ENDDOCUMENT();        # This should be the last executable line in the problem.
</pre> 
Now before I came up with this, I tried the more straightforward regular expression match condition for multiple slashes 
<pre> 
if ((/~~/.*~~/) || ($Answer =~ /~~^/) || ($Answer =~ /~~*~~*/)) 

</pre>

and got all kinds of funky error messages. Was I doing something blindingly stupid or is there some kink in WeBWorK?

TIA, Zig

<| Post or View Comments |>