From WeBWorK_wiki
Revision as of 14:26, 1 February 2008 by Glarose (talk | contribs) (New page: <h2>Matching Problems: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to set up a matching...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Matching Problems: PG Code Snippet

This code snippet shows the essential PG code to set up a matching 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
  $ml = new_match_list();
  "What's the answer to question 1?", "Ans 1",
  "What's the answer to question 2?", "Ans 2",
  "What's the answer to question 3?", "Ans 3"


  # to add extra answers that show up in the
  #    matching list, we would uncomment
  #    (remove the leading "# " from) the two
  #    lines below:
  # $ml->extra("Extra Ans 1", "Extra Ans 2");
  # $ml->choose_extra(2);
  $ml->makeLast("None of the above");

We need make no changes to the description or initialization sections of the problem. In the problem set-up section, we define the matching list object by initializing it with new_match_list(), and then define a set of questions and answers with object->qa().

We can then select a subset of the matching problems to display to any given student by using the object->choose() call. Here, we select two of the three questions to display. We can also add extra answers that are displayed at the end of the answer list, as suggested by the commented out line with the call to extra(). In general, when there are extra answers (that is, when fewer than the full number of matches is chosen to display, or if extra answers are defined), we specify the number of extra answers to show with the choose_extra() call. In the commented out lines here, the choose_extra(2) indicates that two extra answers will be shown, drawn from the unused correct answers and the defined extra answers.

Finally, we can also add an extra answer or answers at the end of the matching list by calling makeLast(), as shown here. This call will add the one answer None of the above; specifying a list of answers (e.g., makeLast('All of the above','None of the above')) will add all of those, in order at the end. The correct answer can be included in the makeLast() call.

  \{ $ml->print_q() \}
  \{ $ml->print_a() \}

We then insert the matching list questions and answers into the problem text section of the problem.

  ANS( str_cmp( $ml->ra_correct_ans ) );

  # the remainder of the code is included to
  #    provide a sensible solution for the
  #    student.

  # the answers to the questions that were
  #    asked, in order, are
  @correctAns = @{$ml->ra_correct_ans};

  # the following becomes necessary if we want
  #    to figure out what questions were asked
  #    so that we can give explanations for 
  #    them.  
  # it's useful to define an array of 
  #    explanations that correspond to the
  #    list of questions we might have asked
  @explanations = (
  "explanation for question 1",
  "explanation for question 2",
  "explanation for question 3"

  # then find the questions that were asked
  @askedQuestions = ();
  foreach $q ( @{$ml->selected_q} ) {
    $i = 0;
    foreach $mq ( @{$ml->questions} ) {
      if ( $q eq $mq ) {
        push(@askedQuestions, $i);
  # now we know which questions were asked,
  #    and can print the corresponding 
  #    explanations for the solution
  The answer to the first question is
  $correctAns[0], because

  The answer to the second question is
  $correctAns[1], because


The correct answers are most easily graded using the old-style answer evaluator str_cmp.

We can get a list of the answers the student had to answer by looking at the array reference found from object->ra_correct_ans(). However, this doesn't tell us what questions from the list that we started were asked of the student, so to explain to the student what the correct answers were in our solution is a bit more complicated.

One way to do this is to use the list of questions that were asked to figure out which of the questions we could have asked showed up in the problem. Then we can show the corresponding explanations in the solution.

To find the answers that were asked, we go through the questions that were asked and compare them with each of the questions that we could have asked, to see which is which. In this example we then save the index of the question and use that to print out the correct explanation.

Problem Techniques Index