WeBWorK Problems

Reduced Fractions in Intervals

Reduced Fractions in Intervals

by Brittni Lorton -
Number of replies: 0

I was reading through this topic here: https://webwork.maa.org/moodle/mod/forum/discuss.php?d=2910 

but am wondering if there has been any updates since 2013 to the overlap of intervals and fraction contexts? I am wanting to have a problem where students must enter their answers in interval notation with reduced fractions. I have a working problem, code below, but I cannot seem to get it to show the error message that one of their fractions is correct but unreduced, though it does mark them wrong. 

Maybe there is a smoother way to achieve this behavior? If not can someone help identify the issue in error messages I am having in this code? Thanks

DOCUMENT();


loadMacros(

  "PGstandard.pl",

  "MathObjects.pl",

  "PGML.pl",

  "AnswerFormatHelp.pl",

  "contextFraction.pl",

  "contextInequalitySetBuilder.pl",

  "parserOneOf.pl",

  "PGcourse.pl",

);


##############################################


Context("Numeric");


$m = random(1,10,1);

$b = non_zero_random(-10,10,1);


$a = random(1,4,1);

$c = random(5,9,1);

$f=Formula("abs($m*x+$b)+$a")->reduce;


Context("Fraction-NoDecimals");

    Context()->parens->set(

       '(' => {type => 'Interval'},

       '[' => {type => 'Interval'},

       '{' => {type => 'Set', removable => 0, emptyOK => 1},

    );

    Context()->constants->add(R => Interval("(-infinity,infinity)"));

    Context()->constants->set(R => {TeX => '{\bf R}'});

    

$left = Fraction("($a-$c-$b)/$m");

$right = Fraction("($c-$a-$b)/$m");


$answer = Compute("[$left,$right]");


##############################################


TEXT(beginproblem());


BEGIN_PGML


Solve the following inequality involving absolute value.  Enter the answer in interval notation using simplified fractions.


[`[$f] \le [$c]`]


END_PGML


BEGIN_TEXT


\{ans_rule(30)\}


END_TEXT


    Context()->flags->set(reduceFractions=>0);

    

    ANS($answer->cmp(

      checker => sub {

        my ($correct, $student, $ans, $nth, $value) = @_;

        $student->context->setError("Your $nth $value contains an unreduced fraction","",undef,undef,$Value::CMP_WARNING)

          if $student->{notReduced};

        return 0 unless $correct == $student;

        my $reduced = 1;

        foreach $x ($student->value) {

          if (Value::classMatch($x,'Fraction') && !$x->isReduced) {

            $reduced = 0;

          }

        }

        $student->{notReduced} = 1 unless $reduced || $ans->{isPreview};

        return $reduced;

      }

    ));