WeBWorK Main Forum

Can't get List of strings for an answer to work in PGML problem...

Can't get List of strings for an answer to work in PGML problem...

by Christian Seberino -
Number of replies: 2
The problem below doesn't accept the two strings for answers.  It accepts
each one if it is the only answer but there seems to be an issue with List
and strings.  

When I ask Webwork to show correct answer I get...

List($strings_1, $strings_2)
(literally)

How fix?



DOCUMENT(); 
loadMacros( 
  "MathObjects.pl", 
  "PGstandard.pl", 
  "PGML.pl", 
  "PGcourse.pl", 
  "parserNumberWithUnits.pl", 
  "contextArbitraryString.pl", 
  "parserMultiAnswer.pl", 
  "parserPopUp.pl", 
  "contextInequalities.pl", 
  "PGgraphmacros.pl", 
); 
TEXT(beginproblem()); 
$showPartialCorrectAnswers = 1; 
###################################################################### 
 
Context("ArbitraryString"); 
@string_answers_1 = ("BC", "CB"); 
$strings_1 = Compute($string_answers_1[0])->cmp(checker => sub { 
        my ($temp1, $response, $temp2) = @_; 
        $response =~ s/^~~s+|~~s+$//g; 
        $response = lc($response); 
        foreach (@string_answers_1) { 
                if ($response eq lc($_)) { 
                        return 1; 
                } 
        } 
 
        return 0; 
}); 

@string_answers_2 = ("DA", "AD"); 
$strings_2 = Compute($string_answers_2[0])->cmp(checker => sub { 
        my ($temp1, $response, $temp2) = @_; 
        $response =~ s/^~~s+|~~s+$//g; 
        $response = lc($response); 
        foreach (@string_answers_2) { 
                if ($response eq lc($_)) { 
                        return 1; 
                } 
        } 
 
        return 0; 
}); 
 
BEGIN_PGML 
What two lengths in the picture below would have to be equal 
to be able to use the SSS Theorem to prove that the two 
triangles are congruent? 
 
[@ image("7.png", 
          width=>505,height=>330,tex_size=>480) @]* 
 
[________________________]{List($strings_1, $strings_2)} 
END_PGML 
 
###################################################################### 
ENDDOCUMENT(); 


In reply to Christian Seberino

Re: Can't get List of strings for an answer to work in PGML problem...

by Davide Cervone -
There are several problems with your approach.

First, your $strings_1 and $strings_2 are not MathObject strings, they are WeBWorK AnswerEvaluators, since you have set them equal to the results of cmp() calls (which return AnswerEvaluators). When you try to pass these to List(), it fails to produce a List object, because you have passed it things that can't be used in a list. It actually produces an error message about this, but PGML traps the error and interprets it to mean that the value in the braces is not a valid Perl command so it uses it as a literal string (as you pointed out).

Second since you are using the ArbitraryString context, you can't use lists, because the ArbitraryString context does no parsing of the student's input whatsoever. Since using the comma as a list separator would be parsing the student answer, that isn't done. This really is an arbitrary string. If you want to have the answer broken up as a list, you would need to do that yourself in a custom checker. So you could use

    $cmp = Compute("BC,AD")->cmp(checker=>sub {
       ... (your code here) ...
    });

    BEGIN_PGML
    [____________]{$cmp}
    END_PGML
but the custom checker would be complicated to write.

Alternatively, you might want to use the contextString.pl context, which allows only string answers, and only the ones your define. Here is one approach:

loadMacros("contextString.pl");

Context("String"); 
Context()->operators->redefine(',', using=>',');  # allow lists of strings
Context()->strings->add(
  "BC" => {caseSensitive=>1}, "CB" => {alias => "BC", caseSensitive=>1},
  "AD" => {caseSensitive=>1}, "DA" => {alias => "AD", caseSensitive=>1},
);

BEGIN_PGML
[________]{"BC,AD"}
END_PGML
Of course, you would need to add whatever other pairs might be entered (I don't have the diagram, so can't tell what they might be). It is possible to add the strings programmatically, for example, to get all pairs of letters from a given list:
loadMacros("contextString.pl","PGML.pl");

Context("String"); 
Context()->operators->redefine(',', using=>',');  # allow lists of strings

@letters = ("A","B","C","D"); $n = scalar(@letters);
foreach $i (0..$n-2) {
  foreach $j ($i+1..$n-1) {
    $AB = $letters[$i].$letters[$j];
    $BA = $letters[$j].$letters[$i];
    Context()->strings->add(
      $AB => {caseSensitive => 1},
      $BA => {caseSensitive => 1, alias => $AB},
    );
  }
}

BEGIN_PGML
[________]{"BC,AD"}
END_PGML