## WeBWorK Problems

### Set Notation with Variables

by Brittni Lorton -
Number of replies: 2
I am working on a simple problem involving operations on sets. I would like to make it so that when the student is asked for something like A \cup B, they are required to enter the { } as well as the items in the set.

I attempted using the Set Class to no success. I believe that is because the Set Class is using real numbers and not variables? Is there a way to use the Set Class, or something similar, with letters, not just real numbers?

Below is the code I currently have that works with strings, but I would like to make it so that the student is required to enter the curly brackets. Any advice?

Thanks,
*Brittni

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
"contextString.pl"
);

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

Context("String");
Context()->operators->redefine(',', using=>','); # allow lists of strings
"a" => {caseSensitive=>1}, "b" => {caseSensitive=>1},
"c" => {caseSensitive=>1}, "d" => {caseSensitive=>1},
"e" => {caseSensitive=>1}, "f" => {caseSensitive=>1},
"g" => {caseSensitive=>1}, "h" => {caseSensitive=>1},
);

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

BEGIN_PGML

Given the sets

[U=\{a,b,c,d,e,f,g,h\}]

[A= \{a, d, g, h\}]

[B=\{b,g,h\}]

[C=\{b,c,d,f\}]

compute the following set operations.

a. [A\cap B=\{] [____________]{"g,h"} [\}]

b. [B\cap C=\{] [____________]{"b"} [\}]
END_PGML

### Re: Set Notation with Variables

by Davide Cervone -
The MathObject Set class is a "Set of Reals" set, not an arbitrary set, and this is rather fundamental to the definition of the class (in particular in how it interoperates with the Interval and Union classes.

But you can arrange to do something that should act like what you are doing by using constants for the various letters, and removing all the content features you don't need. Here is an example:

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
);

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

$context = Context("Interval");$context->flags->set(
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 = ('a'..'h'); foreach$n (0..$#letters) {$context->constants->add($letters[$n] => \$n);
}

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

BEGIN_PGML

Given the sets

[U=\{a,b,c,d,e,f,g,h\}]

[A= \{a, d, g, h\}]

[B=\{b,g,h\}]

[C=\{b,c,d,f\}]

compute the following set operations:

a. [A\cap B=] [____________]{"{g,h}"}

b. [B\cap C=] [____________]{"{b}"}

END_PGML


Here, we use the Interval content (since that is where sets are defined), and modify it to remove all the variables, constants, operators, functions, parentheses, and so on, and only allow set brackets, and commas. We add in constants for each of the letters you want to use (whose values are numbers). We set flags to use the parsed value as the student answer (so the answer will show as the letters rather than as numbers), and add a NumberCheck function that simply causes an error message if the student enters an explicit number. Finally, make error messages reference this as a set (rather than "an interval, union, or set").

All that should mean that the only answers that students are allowed to enter will be sets of letters, with reasonable error messages for everything else. The only message I don't like is if they enter just a letter of list of letters, they will get a message about it not being a set, but looking liking a number. One could work a little harder to fix that, if you feel it is necessary.

Anyway, I think that should get you what you are looking for.