## WeBWorK Main Forum

### Ask students to enter the power set of a set

by Lijuan Cao -
Number of replies: 7

Hello, I want to create some questions on power set. More specifically, the question will ask students to write out the power set. For example, if A = {0, 1}, students would enter {{0}, {1}, {0, 1}, {}}. I created a .pg file to test out this simple example, and I encountered two problems.

1.  I  cannot find a way to include the outer {} as part of the answer.

2. The answer checker does not check for {} when there is only one element in a set, i.e., if a student enters 0, 1, {0, 1}, {}, the answer checker passes.

Is there a way to resolve these issues? My code is as follows. Thank you.

$context = Context("Interval");$answer = Compute("{0}, {1}, {0, 1}, {}");

BEGIN_PGML

Let [A = \{0, 1\}]. [\mathcal P(A) = ][___________________________________]

END_PGML

$showPartialCorrectAnswers = 1; ANS($answer->cmp(entry_type=>"set"));

### Re: Ask students to enter the power set of a set

by Davide Cervone -

To do this properly, one must create a custom context for sets of sets. I have done that in the attached file. See the documentation at the top of the file for more details. The context uses the List object to manage the set of sets. It subclasses the List object and uses braces to create lists; when a list of numbers is created, it turns it into a Set object (so that the set of sets is a List of Sets). There is some checking for repeated entries and some fanciness to make the error reports better, but that is the basic setup.

That means you can use

loadMacros(
"contextSetOfSets.pl"
);

Context("SetOfSets");

$showPartialCorrectAnswers = 1;$S = Compute("{0,1}");
$ans = Compute("{{}, {0}, {1}, {0,1}}"); BEGIN_PGML The power set of [[$S]] is [___________________]{$ans} END_PGML  to get your power-set question. It is possible to set up a name for the empty set (see the documentation in the file). It is also possible to use other symbols in the sets, like letters. To do that, you have to set up constants in the context that represent the symbols you want to use. Set these to be constants for distinct integers, for example, as shown below. You may also want to set a NumberCheck function to disallow entry of numbers rather than letters, and you may want to set the entry_type in order to make the error messages more meaningful. Here is an example that allows sets to consist of the letters A, B, and C: loadMacros( "contextSetOfSets.pl" ); Context("SetOfSets"); @letters = ('A' .. 'C'); Context()->constants->add(map {$letters[$_] =>$_} (0..$#letters)); Context()->flags->set(NumberCheck => sub { my$self = shift;
$self->Error("An element of a set of letters can't be a number"); }); Context()->{cmpDefaults}{List}{entry_type} = "a set of letters";$showPartialCorrectAnswers = 1;

$S = Formula("{A,B}");$ans = Compute("{{}, {A}, {B}, {A,B}}");

BEGIN_PGML
The power set of [[$S]] is [___________________]{$ans}
END_PGML


You can get more letters by simply setting the @letters array to include additional letters, or even make it something like

@letters = ('Red', 'Blue', 'Green');


Note, however, that these are case-sensitive names, and you may wish to add

Context()->constants->set(map {$_ => {TeX => "\text{$_}"}} @letters);


after creating the constants in order to produce better TeX output for them.

A full example is:

loadMacros(
"contextSetOfSets.pl"
);

Context("SetOfSets");

@letters = ('red', 'green', 'blue');
Context()->constants->add(map {$letters[$_] => $_} (0..$#letters));
Context()->constants->set(map {$_ => {TeX => "\text{$_}"}} @letters);
Context()->flags->set(NumberCheck => sub {
my $self = shift;$self->Error("An element of a set of letters can't be a number");
});
Context()->{cmpDefaults}{List}{entry_type} = "a set of letters";

$showPartialCorrectAnswers = 1;$S = Formula("{red, green}");
$ans = Compute("{{}, {red}, {green}, {red, green}}"); BEGIN_PGML The power set of [[$S]] is [___________________]{$ans} END_PGML  I think that should handle your needs (and then some). In reply to Davide Cervone ### Re: Ask students to enter the power set of a set by Lijuan Cao - Thanks a lot, Davide. This new context works and the answer checker displays a proper error message if an answer misses the outer {}. The second issue still exists as the answer checker passes when I entered {0, 1, {0, 1}, {}}. I looked through the .pl file but I am not sure how to make the answer checker check on this. In reply to Lijuan Cao ### Re: Ask students to enter the power set of a set by Davide Cervone - I thought I had that working. But I went through several different approaches, and might have checked it in an earlier one. In any case, I've updated the .pl file in my previous message. See if that works. In reply to Davide Cervone ### Re: Ask students to enter the power set of a set by Lijuan Cao - Yes, the updated version works perfect! Thanks again! In reply to Davide Cervone ### Re: Ask students to enter the power set of a set by Cindy Loten - Hello, I was looking at your example for making the power set of {red,green}. I added the following code so potential students could see the correct answer : BEGIN_PGML_SOLUTION The answer is [$ans].
END_PGML_SOLUTION

However, the solution displayed is

( Instructor solution preview: show the student solution after due date. )

The answer is {{}, {0}, {1}, {0,1}}.

Can you let me know why the answer is displayed incorrectly?  Thank-you!

Cindy

### Re: Ask students to enter the power set of a set

by Dan Christensen -

I'm using the contextSetofSets.pl file, but when I try entering the empty set as {} I get the error message:

(it looks like a set)

An example .pg file is attached.

Thanks for any help!