## Forum archive 2000-2006

### Michael Gage - Matching list example ### Michael Gage - Matching list example

by Arnold Pizer -
Number of replies: 0
 Matching list example topic started 5/8/2000; 3:52:04 PMlast post 9/10/2001; 9:02:51 PM Michael Gage - Matching list example 5/8/2000; 3:52:04 PM (reads: 5226, responses: 3)

## 'Matching list' basic example

(for more advanced information on matching lists, see Match.pm) To obtain the problem:

(1 pt) rochesterLibrary/setMAAtutorial/matchinglistexample.pg

Matching list example

Place the letter of the derivative next to each function listed below:

1. 2. 3. 4. A. B. C. D. Let's print the questions again, but insist that the first two questions (about sin and cos) always be included. Here is a second way to format this question, using tables:

 1. 2. 3. A. B. C. And below is yet another way to enter a table of questions and answers:

 1. 2. 3. A. B. C. D. The derivative is not provided

WARNINGS
µ¦å{h­
DOCUMENT();        # This should be the first executable line in the problem.loadMacros(           "PGbasicmacros.pl",           "PGchoicemacros.pl",           "PGanswermacros.pl",           );TEXT(beginproblem(), $BR,$BBOLD, "Matching list example", $EBOLD,$BR,$BR);# Since this is a matching question, we do not usually wish to tell students# which parts of the matching question have been answered correctly and which# areincorrect. That is too easy. To accomplish this we set the following# flag to zero.$showPartialCorrectAnswers = 0;# Make a new match list$ml = new_match_list();# enter questions and matching answers$ml -> qa ("$$\sin(x)$$",        # Notice the use of the LateX construction"$$\cos(x)$$",                # for math mode: $$...$$ and the use of TeX"$$\cos(x)$$",        # symbols such as \sin and \tan."$$-\sin(x)$$","$$\tan(x)$$","$$\sec^2(x)$$",      # Remember that in these strings we are                                                 # only specifying typography,via TeX,"$$x^{20}$$",         #not any calculational rules."$$20x^{19}$$","$$\sin(2x)$$","$$2\cos(2x)$$","$$\sin(3x)$$","$$3\cos(3x)$$");# Calculate coefficients for another question$b=random(2,5);$exp= random(2,5);$coeff=$b*$exp;$new_exp = $exp-1;# Store the question and answers in the match list object.$ml -> qa (    '$${b}x^exp$$',    '$${coeff}x^{new_exp}$$',);# Add another example$b2=random(2,5);$exp2= random(2,5);$coeff2=$b2*$exp;$new_exp2 = $exp-1;$ml -> qa ("$${b2}x^exp2$$","$${coeff2}x^{new_exp2}$$",);# Choose four of the question and answer pairs at random.$ml ->choose(4); # Using choose(8) would choose all eight questions,# but the order of the questions and answers would be# scrambled.# The following code is needed to make the enumeration work right within tables# when LaTeX output is being used.# It is an example of the powerful tools of TeX and perl which are available# for each PG problem author. # Once we figure out the best way to protect enumerated lists automatically# we will include it in the tables macro. Meantime, it is better to have# have to do it by hand, rather than to have the wrong thing done automatically.$BSPACING = MODES( TeX               => '\hbox to .5\linewidth {\hspace{0.5cm}\vbox {',                                   HTML            =>' ',                                   Latex2HTML => ' ');$ESPACING = MODES(TeX => '}}', HTML =>'', Latex2HTML => '');sub protect_enumerated_lists { my @in = @_; my @out = (); foreach my$item (@in) {                push(@out, $BSPACING .$item . $ESPACING); } @out;}# End of code for protecting enumerated lists in TeX.# Now print the text using$ml->print_q for# the questions and $ml->print_a to print the answers.BEGIN_TEXT$PARPlace the letter of the derivative next to each function listed below: $BR\{$ml -> print_q \}$PAR\{$ml -> print_a \}$PAREND_TEXTANS(str_cmp($ml->ra_correct_ans )   ) ;# insist that the first two questions (labeled 0 and 1) are always included$ml ->choose([0,1],1);BEGIN_TEXTLet's print the questions again, but insist that thefirst two questions (about sin and cos) always be included.Here is a second way to format this question, using tables:$PAR\{begintable(2)\}\{row(protect_enumerated_lists( $ml->print_q,$ml -> print_a) )\}\{endtable()\}$PARAnd below is yet another way to enter a table of questions and answers:$PAREND_TEXTANS(str_cmp( $ml->ra_correct_ans ) ) ;# Finally add a last answer$ml ->makeLast("The derivative is $BR not provided");BEGIN_TEXT \{ begintable(2) \} \{ row( protect_enumerated_lists($ml->print_q, $ml ->print_a))\} \{endtable()\}END_TEXT# Enter the correct answers to be checked against the answers to the students.ANS(str_cmp($ml->ra_correct_ans )   ) ;ENDDOCUMENT();

1. Since this is a matching questions, we do not usually wish to tell students which parts of the matching question have been answered correctly and which are incorrect. That is too easy. To accomplish this we set the following flag $showPartialCorrectAnswers to 0 (line 11). 2. Saying that $ml$ml is a scalar variable which contains a pointer to the match list object, but you can think of the match list object as being shoe horned into the variable $ml. You need to remember that $ml Some people use the convention$o_ml to remind them that the variable contains an object, but for short problems that is probably not necessary.

An object contains both data (in this case the list of questions and answers) and subroutines (called methods) for manipulating that data.

3. The construction $ml ->qa(..list of alternating questions and matching answers ...). Asks the object $ml to store the matching questions and answers given in the argument in its private data (lines 35 and 45). Note that that you can call qa() more than once with a match list and it will add new questions. This is not true with all objects. Some objects will overwrite previous questions if qa() is used more than once.

4. The construction $ml->choose(3) asks the object to choose three question/answer pairs from its private data and 5. $out = $ml->print_q() asks the object to store a string of the formatted questions in the variable $out. Calling \{ $ml->print_q() \} within BEGIN_TEXT / END_TEXT  statements will just append the formatted questions to the ouput text that the student sees (line 60). 6. $out = $ml->print_a() asks the object to store a string of the formatted answers in the variable $out.  \{ $ml->print_a() \}  can be used just like above to append a string of the formatted answers to the output text seen by the student (line 62). 7. ANS is needed to compare the students answers to the correct answers (line 66). 8. The answer evaluator str_cmp is just one of many possible answer evaluators from which to choose, each of which specifies a different method for comparing the correct answers to the students answers. str_cmp is almost always used with matching lists since the correct answers will always be a letter indicating one of the possible answers (line 66). <| Post or View Comments |> David Etlinger - Re: Matching list example 5/31/2000; 1:27:50 PM (reads: 4027, responses: 1) There is a small error in the code as written here: the lines to print out the questions and answers should read {$ml -> print_q } { $ml -> print_a } (with the backslashes). <| Post or View Comments |> Michael Gage - Re: Matching list example 6/5/2000; 11:58:08 AM (reads: 4805, responses: 0) Got it. Thanks. <| Post or View Comments |> Michael Gage - Re: Matching list example 9/10/2001; 9:02:51 PM (reads: 5734, responses: 0) ## 'Matching list' basic example (for more advanced information on matching lists, see Match.pm) (1 pt) rochesterLibrary/setMAAtutorial/matchinglistexample.pg Matching list example Place the letter of the derivative next to each function listed below: 1. 2. 3. 4. A. B. C. D. Let's print the questions again, but insist that the first two questions (about sin and cos) always be included. Here is a second way to format this question, using tables:  1. 2. 3. A. B. C. And below is yet another way to enter a table of questions and answers:  1. 2. 3. A. B. C. D. The derivative is not provided WARNINGS µ¦å{h­ DOCUMENT(); # This should be the first executable line in the problem.loadMacros( "PGbasicmacros.pl", "PGchoicemacros.pl", "PGanswermacros.pl", );TEXT(beginproblem(),$BR,$BBOLD, "Matching list example",$EBOLD, $BR,$BR);# Since this is a matching question, we do not usually wish to tell students# which parts of the matching question have been answered correctly and which# areincorrect.  That is too easy.  To accomplish this we set the following# flag to zero.$showPartialCorrectAnswers = 0;# Make a new match list$ml = new_match_list();# enter questions and matching answers$ml -> qa ("$$\sin(x)$$", # Notice the use of the LateX construction"$$\cos(x)$$", # for math mode: $$...$$ and the use of TeX"$$\cos(x)$$", # symbols such as \sin and \tan."$$-\sin(x)$$","$$\tan(x)$$","$$\sec^2(x)$$", # Remember that in these strings we are # only specifying typography,via TeX,"$$x^{20}$$", #not any calculational rules."$$20x^{19}$$","$$\sin(2x)$$","$$2\cos(2x)$$","$$\sin(3x)$$","$$3\cos(3x)$$");# Calculate coefficients for another question$b=random(2,5);$exp= random(2,5);$coeff=$b*$exp;$new_exp =$exp-1;# Store the question and answers in the match list object.$ml -> qa ( '$${b}x^exp$$', '$${coeff}x^{new_exp}$$',);# Add another example$b2=random(2,5);$exp2= random(2,5);$coeff2=$b2*$exp;$new_exp2 =$exp-1;$ml -> qa ("$${b2}x^exp2$$","$${coeff2}x^{new_exp2}$$",);# Choose four of the question and answer pairs at random.$ml ->choose(4); # Using choose(8) would choose all eight questions,# but the order of the questions and answers would be# scrambled.# The following code is needed to make the enumeration work right within tables# when LaTeX output is being used.# It is an example of the powerful tools of TeX and perl which are available# for each PG problem author. # Once we figure out the best way to protect enumerated lists automatically# we will include it in the tables macro.  Meantime, it is better to have# have to do it by hand, rather than to have the wrong thing done automatically.$BSPACING = MODES( TeX => '\hbox to .5\linewidth {\hspace{0.5cm}\vbox {', HTML =>' ', Latex2HTML => ' ');$ESPACING = MODES(TeX => '}}', HTML =>'', Latex2HTML => '');sub protect_enumerated_lists {        my @in = @_;        my @out = ();        foreach my $item (@in) { push(@out,$BSPACING . $item .$ESPACING);        }        @out;}# End of code for protecting enumerated lists in TeX.# Now print the text using $ml->print_q for# the questions and$ml->print_a to print the answers.BEGIN_TEXT$PARPlace the letter of the derivative next to each function listed below:$BR\{ $ml -> print_q \}$PAR\{$ml -> print_a \}$PAREND_TEXTANS(str_cmp( $ml->ra_correct_ans ) ) ;# insist that the first two questions (labeled 0 and 1) are always included$ml ->choose([0,1],1);BEGIN_TEXTLet's print the questions again, but insist that thefirst two questions (about sin and cos) always be included.Here is a second way to format this question, using tables:$PAR\{begintable(2)\}\{row(protect_enumerated_lists($ml->print_q, $ml -> print_a) )\}\{endtable()\}$PARAnd below is yet another way to enter a table of questions and answers:$PAREND_TEXTANS(str_cmp($ml->ra_correct_ans )   ) ;# Finally add a last answer$ml ->makeLast("The derivative is$BR not provided");BEGIN_TEXT    \{ begintable(2) \}    \{ row(   protect_enumerated_lists($ml->print_q,$ml ->print_a))\}    \{endtable()\}END_TEXT# Enter the correct answers to be checked against the answers to the students.ANS(str_cmp( $ml->ra_correct_ans ) ) ;ENDDOCUMENT();  1. Since this is a matching questions, we do not usually wish to tell students which parts of the matching question have been answered correctly and which are incorrect. That is too easy. To accomplish this we set the following flag $showPartialCorrectAnswers to 0 (line 15).
2. Saying that $ml$ml is a scalar variable which contains a pointer to the match list object, but you can think of the match list object as being shoe horned into the variable $ml. You need to remember that $ml

Some people use the convention $o_ml to remind them that the variable contains an object, but for short problems that is probably not necessary. An object contains both data (in this case the list of questions and answers) and subroutines (called methods) for manipulating that data. 3. The construction $ml ->qa(..list of alternating questions and matching answers ...). Asks the object $ml to store the matching questions and answers given in the argument in its private data (lines 44 to 58, 69.. and 78..). Note that that you can call qa() more than once with a match list and it will add new questions. This is not true with all objects. Some objects will overwrite previous questions if qa() is used more than once. 4. The construction $ml->choose(4) asks the object to choose three question/answer pairs from its private data and
5. $out =$ml->print_q() asks the object to store a string of the formatted questions in the variable $out. Calling \{$ml->print_q() \} within BEGIN_TEXT / END_TEXT  statements will just append the formatted questions to the ouput text that the student sees (line 119).
6. $out =$ml->print_a() asks the object to store a string of the formatted answers in the variable $out.  \{$ml->print_a() \}  can be used just like above to append a string of the formatted answers to the output text seen by the student (line 121).
7. ANS is needed to compare the students answers to the correct answers (line 146).
8. The answer evaluator str_cmp is just one of many possible answer evaluators from which to choose, each of which specifies a different method for comparing the correct answers to the students answers. str_cmp is almost always used with matching lists since the correct answers will always be a letter indicating one of the possible answers (line 146).

<| Post or View Comments |>