From WeBWorK_wiki
Revision as of 16:20, 18 January 2008 by Glarose (talk | contribs)
Jump to navigation Jump to search

Lists of Answers: PG Code Snippet

This code snippet shows the essential PG code to check lists of objects as answers to a problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
  $factors = List(Compute("x+2"),Compute("x+3"));
  $roots = List( -3, -2 );

We need make no changes or additions to the tagging and description section of the PG file, or to the problem initialization section (unless we need to load some macros for the type of problem that we're creating). In the problem set-up section of the file, we include the definition of the list(s) that we're expecting as an answer.

Note that the argument of the List call are the objects in the list, which can be any MathObjects. Here we create a list of Formulas and a list of Reals (the numbers that we use in the second list will be promoted to Real MathObjects when the List is created).

  What are the factors of \(x^2 + 5 x + 6\)?
  Factors = \{ ans_rule(25) \}
  ${BITALIC}(Enter the factors as a comma-separated
  What are the roots of this equation?
  Roots = \{ ans_rule(15) \}
  ${BITALIC}(Enter the roots in a comma-separated
  list, ${BBOLD}ordered from smallest to 

In the text section of the problem, we ask for the answers as we'd expect. It's generally a good idea to make sure that it's clear what we expect students to enter (in this case, a comma-separated list). To point out the obvious, there's no reason in this case to make only one of the requested lists have a specific order... except that it lets us see how to do it in this example problem.

  ANS( $factors->cmp() );
  ANS( $roots->cmp( ordered=>1 );

Then, in the answer and solution section of the problem we can just check the answers against the correct List answers. To force the students' list answers to match the order of the correct answer, we include the ordered=>1 flag in the cmp() call.

Problem Techniques Index