## WeBWorK Main Forum

### Set class does not check {}

by Lijuan Cao -
Number of replies: 3

Hello, I am trying to use the Set class and I noticed that the answer checker will not mark the answer wrong if {} is missing. Is there a way to require {} in student's answer? Thank you.

DOCUMENT();

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

TEXT(beginproblem());

###########################
# Setup
Context("Interval");
@nums =(1..10);

@aNums = @nums[NchooseK(10, 6)];
@bNums = @nums[NchooseK(10, 4)];

$A = Set(@aNums);$B = Set(@bNums);
$aString = join(", ", num_sort(@aNums));$bString = join(",", num_sort(@bNums));

$AUB = Union("$A U $B"); ########################### # Main text BEGIN_PGML Let [A = \{[$aString]\}, B = \{[$bString]\}. A \cup B = ] [______________________] END_PGML ############################ # Answer evaluation$showPartialCorrectAnswers = 1;

ANS( $AUB->cmp( removeParens=>0, implicitList=>0, showParenHints=>1, showLengthHints=>1, requireParenMatch=>1, )); ENDDOCUMENT(); In reply to Lijuan Cao ### Re: Set class does not check {} by Davide Cervone - The problem turns out to be in the Union answer checker (which uses the list checker internally, and ends up having accidentally bypassed the type checking that is usually performed). One solution is to make sure that your checker is using a Set rather than a Union (since the union is a set). That can be done in several ways. Oner is to use Set() rather than Union(): $AUB = Set("$A U$B");


Another would be to use the reduce() method to simplify the union into a set:

$AUB = Union($A,$B)->reduce;  Another would be to use the overloaded + operator, which acts as union on operands that are sets: $AUB = $A +$B;


Any of this will result in $AUB being a set rather than a union. A couple of coding suggestions: 1. You don't need $aString and $bString, as PGML will produce the proper output for sets. So you can use Let [A = [$A]], [B = [$B]]. Then [A \cup B = ] [______________________]  2. I would recommend separate math delimiters for the three expressions above, rather than including the commas and periods within the math. The commas and periods are part of the sentence, not the math, and so it is inappropriate to include them in the math itself. 3. You don't need to use a separate ANS() call, but can just put the $AUB in the PGML. None of the values you set are needed, here.

Let [A = [$A]], [B = [$B]]. Then [A \cup B = ] [______________________]{\$AUB}


### Re: Set class does not check {}

by Lijuan Cao -

I tested all three solutions that you mentioned, for some reason, the first two check for {}, and third one does not. I will stick with the first two for now.

Also, I was able to remove the string variables and simplify the answer checking process.

### Re: Set class does not check {}

by Nathan Wallach -
There is a PR by Davide with a corrected version of the Union answer checker at https://github.com/openwebwork/pg/pull/492 which is likely to make it into WW 2.16. Until the, systems can be manually patched or the workarounds above can be used.