next up previous contents
Next: Matching Problems Up: WeBWorK Newbie Guide - Previous: Sample Problem 2   Contents

Matching Problems -- via scripts

In this section we build a generic matching question using the functions sub match_questions_list (@questions) or sub match_questions_list_varbox($length, @questions) which are found in /var/webwork/system/courseScripts/PGchoicemacros.pl. In the next section, we build a matching problem using object-oriented programming techniques.

The format of this problem is remarkably sensitive to changes. After the example, I will be explicit as to where pitfalls seem to lie. The problem itself is reasonably self-documenting, so I will presume you have read the example before trying to make sense of the remarks which follow.

## -*- perl -*- ##
## Line above puts emacs in perl mode
##
## Description  
##   Matching Problem Example
## EndDescription

DOCUMENT();
loadMacros("PG.pl",
           "PGbasicmacros.pl",
           "PGchoicemacros.pl",
           "PGanswermacros.pl",
           "PGauxiliaryFunctions.pl",
           "PGgraphmacros.pl");
 
## Do NOT show partial correct answers
$showPartialCorrectAnswers = 0;

## Generate an array of questions
@questions = ("R stands for", 
            "O stands for", 
            "Y stands for",
            "G stands for", 
            "B stands for",
            "I stands for", 
            "V stands for"
             );

## Generate a corresponding array of answers
@answers = ("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");

## A list consisting of 0, 1, .. , 6 in "random" order.
@permutation = NchooseK(7,7);

## Ok, we are ready to begin...
##

TEXT(beginproblem());

BEGIN_TEXT
$BR 
The first problem below is a matching problem
using seven questions and corresponding answers.  
$BR

$BBOLD Matching Problem 1: $EBOLD In the colors of the visible light
spectrum, the acronym ROYGBIV is often used.  $BR 
END_TEXT

TEXT(
     match_questions_list(@questions),
     OL(@answers[@permutation])
    );

ANS(str_cmp([@ALPHABET[invert(@permutation)]]));


## Now we will choose a subset of the possible problems to display

## Choose 4 integers from 0, 1, ... , 6
@subset = NchooseK(7,4);

@subset_of_questions = @questions[@subset];
@subset_of_answers = @answers[@subset];
@new_permutation = NchooseK(4,4);


BEGIN_TEXT 
$PAR
The second problem below is a matching problem
using four of seven possible questions and corresponding answers.  
$BR

$BBOLD Matching Problem 2: $EBOLD In the colors of the visible light
spectrum, the acronym ROYGBIV is often used.  $BR 
END_TEXT

TEXT(
     match_questions_list(@subset_of_questions),
     OL(@subset_of_answers[@new_permutation])
    );

ANS(str_cmp([@ALPHABET[invert(@new_permutation)]]));

ENDDOCUMENT();

Remarks: A number of remarks are in order here. The heart of the first problem is contained in the lines:

TEXT(match_questions_list(@questions),
     OL(@answers[@permutation]));

ANS(str_cmp([@ALPHABET[invert(@permutation)]]));

The function match_questions_list simply enumerates and lists the questions, providing an answer box in front of each question. If the answers which are to be supplied are longer than two or three characters, you should use the match_questions_list_varbox function which takes two arguments, the first of which is the length of the box, and the second the list of questions.

The line OL(@answers[@permutation]) displays an ordered list of the answers labeled A, B, C, ...but permuted from their original order by the @permutation. To verify the answer is correct, one uses ANS(str_cmp([@ALPHABET[invert(@permutation)]]));. That is, when the answers are listed on the web page, $ALPHABET[$i] is associated with $answer[$ \sigma$($i)], where $ \sigma$ is the permutation. Thus $ALPHABET[ $ \sigma^{-1}$($i)] is associated with $answer[$i], the correct answer to $question[$i].

Now for the fussing. First observe that the math_questions_list construction wants to be in the TEXT(...); context, and not passed through EV2 or EV3.

Second, observe that the answer is in a slightly different form than you may have expected. We are using str_cmp instead of something like std_str_cmp, and the argument is a list indicated by the extra [ ]'s surrounding @ALPHABET[invert(@permutation)].


next up previous contents
Next: Matching Problems Up: WeBWorK Newbie Guide - Previous: Sample Problem 2   Contents
Thomas R. Shemanske 2002-03-05