WeBWorK Problems

Packaging some code

Packaging some code

by Alex Jordan -
Number of replies: 2
I have some code that Davide Cervone provided for me that I am using to check that basic algebra answers have their fractions reduced. The Fraction context falls short for this task in some cases. For example, it won't help me require 2x/6 be reduced to x/3, and it won't help me require x=2/4 be reduce to x=1/2 when I am using the Assignment parser.

As I am using it now, the code comes in two parts:
  • I load a macro file that subclasses the division, making a weird new meaning for '/'. The code is short:

    package bizarro::BOP::divide;
    our @ISA = ('Parser::BOP::divide');

    # Return a/(b^3(2+sin(b)) when bizarroDiv is set
    sub _eval {
    my $self = shift;
    my $context = $self->context;
    my ($a,$b) = @_;
    if ($context->flag("bizarroDiv")) {
    return $a/(($b)**3*(2+CORE::sin($b)));}
    else {
    return $a / $b;

    sub call {(shift)->_eval(@_)}

  • Then in the problem, immediately following a context declaration, I have more code to make the context use the bizarro division during answer checking:

    '/' => {class => 'bizarro::BOP::divide', isCommand => 1},
    ' /' => {class => 'bizarro::BOP::divide', isCommand => 1},
    '/ ' => {class => 'bizarro::BOP::divide', isCommand => 1},
    '//' => {class => 'bizarro::BOP::divide', isCommand => 1},

    Context()->{cmpDefaults}{Formula}{checker} = sub {
    my ($correct,$student,$ans) = @_;
    return 0 if $ans->{isPreview} || $correct != $student;
    $student = $ans->{student_formula};
    $correct = $correct->{original_formula} if defined $correct->{original_formula};
    Context()->flags->set(bizarroDiv=> 1);
    delete $correct->{test_values}, $student->{test_values};
    my $OK = ($correct == $student); # check if equal when / is replace by bizarro /
    Context()->flags->set(bizarroDiv=> 0);
    Value::Error("Your answer is not simplified") unless $OK;
    return $OK;

Now when say, the expected answer is "x/3", and a student answers with "2x/6", they are asked to simplify more.

I am having trouble packaging this - probably I just don't know the right perl. Is there a way for me to put all this code into the same .pl file, and then be able to activate the second part with a single command? I would be able to do something like:


ActivateBizarroDivision(); #replacing my second block of code above

$ans = Formula("x/3");

Note that I want to be able to use this alongside several contexts, so I do not want to make a context macro file to implement this. And I can't simply throw the code in to my existing .pl file, because the context modification parts need to happen after I have declared a context.

In reply to Alex Jordan

Re: Packaging some code

by Alex Jordan -
Related to this, I am trying to make a bizarro power so that I can require students to enter answers in the form 16^2 instead of 256. But when I simply change the code above (which works great for bizarro multiplication) to make it for exponentiation, then when a student enters an answer (say 16^2) there is an error message:

syntax error at (eval 4955) line 6, near "**("

I am attaching the file parserBizarroPower.pl and a problem that tries to make use of it. The problem file is in the middle of being debugged, if it seems like there is a lot of unnecessary bits to it.