WeBWorK Problems

Answer is a list of sets

Re: Answer is a list of sets

by Davide Cervone -
Number of replies: 0

This discussion may be more applicable here, because it uses actual sets, not lists, as in your example.

For instance:

###########################
# Setup

$context = Context("Interval");

$context->flags->set(
  formatStudentAnswer => 'parsed',
  NumberCheck => sub {
    my $self = shift;
    $self->Error('Numbers are not allowed in this answer');
  }
);
$context->variables->clear;
$context->functions->disable('All');
$context->operators->undefine($context->operators->names);
$context->operators->redefine(',');
$context->strings->clear;
$context->parens->undefine('(','[');
$context->constants->clear;
$context->{cmpDefaults}{Set} = {cmp_class => "a set"};

@letters = ('alpha', 'beta', 'gamma', 'delta');
foreach $n (0..$#letters) {
  $context->constants->add($letters[$n] => $n);
  $context->constants->set($letters[$n] => {TeX => "\$letters[$n]"});
}

$answer2 = Compute("{alpha,beta}, {gamma,delta}");


###########################
# Main text

BEGIN_PGML

Enter [| {alpha,beta}, {gamma,delta} |] [_____________________]{$answer2}

END_PGML

Lets you enter the two sets in any order, with the elements in any order. You are not allowed to do any computations within the sets, however, so if your matrix or permutation questions need that sort of thing, then this won't work as is.

The Sets MathObject is fundamentally a set of Reals. It would be possible to make an object that is a set of more arbitrary elements, as long as the elements have a natural ordering. One could probably convert the current Set object implementation to such an arbitrary set without too much difficulty. you can probably get away without re-implementing the Union object since you don't need intervals and could just do finite set operations. you might need to do some subclassing in the Parser item classes as well, but it's been a while since I worked with them, so don't remember how abstract they are.