Difference between revisions of "MatchingProblems"

From WeBWorK_wiki
Jump to navigation Jump to search
(add historical tag and give links to newer problems.)
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<h2>Matching Problems: PG Code Snippet</h2>
 
  +
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with many other problems:</p>
  +
* [https://openwebwork.github.io/pg-docs/sample-problems/Misc/Matching.html Original matching problem]
  +
* [https://openwebwork.github.io/pg-docs/sample-problems/Algebra/MatchingAlt.html A newer matching problem]
  +
  +
<h2>Matching Problems</h2>
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<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 problem. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
+
<em>This shows the essential PG code to set up a matching problem.</em>
 
</p>
 
</p>
   
Line 15: Line 22:
 
</tr>
 
</tr>
 
<tr valign="top">
 
<tr valign="top">
<td style="background-color:#ffffdd;border:black 1px dashed;">
+
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
DOCUMENT();
  +
 
loadMacros(
 
loadMacros(
 
"PG.pl",
 
"PG.pl",
Line 22: Line 31:
 
"PGchoicemacros.pl",
 
"PGchoicemacros.pl",
 
"PGanswermacros.pl",
 
"PGanswermacros.pl",
  +
"unionTables.pl",
 
);
 
);
  +
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
<td style="background-color:#ffffcc;padding:7px;">
+
<td style="background-color:#ccffcc;padding:7px;">
 
<p>
 
<p>
Initialization: We must load all of these macros.
 
  +
<b>Initialization:</b> All of these macros are required, except for <code>unionTables.pl</code>, which is only needed if side-by-side matching lists are desired (see the Main Text section for more details).
 
</p>
 
</p>
 
</td>
 
</td>
Line 36: Line 48:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$ml = new_match_list();
+
$ml = new_match_list();
$ml-&gt;qa(
+
$ml-&gt;qa(
"What's the answer to question 1?", "Ans 1",
+
"What's the answer to question 1?", "Ans 1",
"What's the answer to question 2?", "Ans 2",
+
"What's the answer to question 2?", "Ans 2",
"What's the answer to question 3?", "Ans 3"
+
"What's the answer to question 3?", "Ans 3"
);
+
);
   
$ml-&gt;choose(2);
 
  +
# use pop-up-lists
  +
$ml->rf_print_q(~~&pop_up_list_print_q);
  +
# you may need to add more letters D=>"D", etc.
  +
$ml->ra_pop_up_list([No_answer=>"?",A=>"A",B=>"B",C=>"C"]);
   
# to add extra answers that show up in the
 
  +
$ml-&gt;choose(3);
# matching list, we would uncomment
 
  +
# (remove the leading "# " from) the two
 
  +
# to add extra answers that show up in the
# lines below:
 
  +
# matching list, we would uncomment
# $ml-&gt;extra("Extra Ans 1", "Extra Ans 2");
 
  +
# (remove the leading "# " from) the two
# $ml-&gt;choose_extra(2);
 
  +
# lines below:
  +
# $ml-&gt;extra("Extra Ans 1", "Extra Ans 2");
  +
# $ml-&gt;choose_extra(2);
 
 
$ml-&gt;makeLast("None of the above");
+
$ml-&gt;makeLast("None of the above");
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
Set-up:
 
  +
<b>Setup:</b>
 
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 <code>new_match_list()</code>, and then define a set of questions and answers with <code>object-&gt;qa()</code>.
 
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 <code>new_match_list()</code>, and then define a set of questions and answers with <code>object-&gt;qa()</code>.
 
</p>
 
</p>
Line 71: Line 88:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
\{ $ml-&gt;print_q() \}
+
\{ $ml-&gt;print_a() \}
+
\{ $ml-&gt;print_q() \}
END_TEXT
+
$BR
  +
\{ $ml-&gt;print_a() \}
  +
  +
END_TEXT
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
Main Text: We then insert the matching list questions and answers into the problem text section of the problem.
 
  +
<b>Main Text:</b>
  +
We then insert the matching list questions and answers into the problem text section of the problem.
  +
</p>
  +
<p>
  +
To display the list of questions and answers side-by-side, in the initialization section load the Union macros with <code>loadMacros("unionTables.pl");</code> and in the main text use
  +
<pre>
  +
BEGIN_TEXT
  +
\{ ColumnMatchTable($ml) \}
  +
END_TEXT
  +
</pre>
  +
or
  +
<pre>
  +
BEGIN_TEXT
  +
\{ColumnTable(
  +
$PAR.$ml->print_q.$PAR,
  +
$ml->print_a,
  +
indent => 0, separation => 30, valign => "TOP"
  +
)\}
  +
$PAR
  +
END_TEXT
  +
</pre>
 
</p>
 
</p>
 
</td>
 
</td>
Line 85: Line 122:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( str_cmp( $ml-&gt;ra_correct_ans ) );
 
  +
# no credit until all answers are correct
  +
install_problem_grader(~~&std_problem_grader);
  +
$showPartialCorrectAnswers = 0;
   
# the remainder of the code is included to
 
  +
ANS( str_cmp( $ml-&gt;ra_correct_ans ) );
# provide a sensible solution for the
 
# student.
 
   
# the answers to the questions that were
+
# the remainder of the code is included to
# asked, in order, are
+
# provide a sensible solution for the
@correctAns = @{$ml-&gt;ra_correct_ans};
+
# student.
   
# the following becomes necessary if we want
 
  +
# the answers to the questions that were
# to figure out what questions were asked
 
  +
# asked, in order, are
# so that we can give explanations for
 
  +
@correctAns = @{$ml-&gt;ra_correct_ans};
# 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
 
  +
# the following becomes necessary if we want
@askedQuestions = ();
 
  +
# to figure out what questions were asked
foreach $q ( @{$ml-&gt;selected_q} ) {
 
  +
# so that we can give explanations for
$i = 0;
+
# them.
foreach $mq ( @{$ml-&gt;questions} ) {
+
# it's useful to define an array of
if ( $q eq $mq ) {
+
# explanations that correspond to the
push(@askedQuestions, $i);
+
# list of questions we might have asked
last;
+
@explanations = (
}
+
"explanation for question 1",
$i++;
+
"explanation for question 2",
  +
"explanation for question 3"
  +
);
  +
  +
# then find the questions that were asked
  +
@askedQuestions = ();
  +
foreach $q ( @{$ml-&gt;selected_q} ) {
  +
$i = 0;
  +
foreach $mq ( @{$ml-&gt;questions} ) {
  +
if ( $q eq $mq ) {
  +
push(@askedQuestions, $i);
  +
last;
 
}
 
}
}
 
  +
$i++;
# now we know which questions were asked,
 
  +
}
# and can print the corresponding
 
  +
}
# explanations for the solution
 
  +
# now we know which questions were asked,
SOLUTION(EV3(&lt;&lt;'END_SOLUTION'));
 
  +
# and can print the corresponding
$PAR SOLUTION $PAR
 
  +
# explanations for the solution
  +
#SOLUTION(EV3(&lt;&lt;'END_SOLUTION'));
  +
BEGIN_SOLUTION
  +
$PAR SOLUTION $PAR
 
 
The answer to the first question is
+
The answer to the first question is
$correctAns[0], because
+
$correctAns[0], because
$explanations[$askedQuestions[0]].
+
$explanations[$askedQuestions[0]].
  +
  +
$PAR
  +
The answer to the second question is
  +
$correctAns[1], because
  +
$explanations[$askedQuestions[1]].
   
$PAR
 
  +
END_SOLUTION
The answer to the second question is
 
$correctAns[1], because
 
$explanations[$askedQuestions[1]].
 
   
END_SOLUTION
 
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
Answer evaluation and solution:
+
<b>Answer evaluation and solution:</b>
 
The correct answers are most easily graded using the old-style answer evaluator <code>str_cmp</code>.
 
The correct answers are most easily graded using the old-style answer evaluator <code>str_cmp</code>.
 
</p>
 
</p>

Latest revision as of 08:15, 16 July 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with many other problems:

Matching Problems

This shows the essential PG code to set up a matching problem.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"unionTables.pl",
);

TEXT(beginproblem());

Initialization: All of these macros are required, except for unionTables.pl, which is only needed if side-by-side matching lists are desired (see the Main Text section for more details).

$ml = new_match_list();
$ml->qa(
"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"
);

# use pop-up-lists
$ml->rf_print_q(~~&pop_up_list_print_q);
# you may need to add more letters D=>"D", etc. 
$ml->ra_pop_up_list([No_answer=>"?",A=>"A",B=>"B",C=>"C"]);

$ml->choose(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");

Setup: 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.

BEGIN_TEXT

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

END_TEXT

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

To display the list of questions and answers side-by-side, in the initialization section load the Union macros with loadMacros("unionTables.pl"); and in the main text use

BEGIN_TEXT
\{ ColumnMatchTable($ml) \}
END_TEXT

or

BEGIN_TEXT
\{ColumnTable(
$PAR.$ml->print_q.$PAR,
$ml->print_a,
indent => 0, separation => 30, valign => "TOP"
)\}
$PAR
END_TEXT

# no credit until all answers are correct
install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 0;

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);
      last;
    }
    $i++;
  } 
}
# now we know which questions were asked,
#    and can print the corresponding 
#    explanations for the solution
#SOLUTION(EV3(<<'END_SOLUTION'));
BEGIN_SOLUTION
$PAR SOLUTION $PAR
  
The answer to the first question is
$correctAns[0], because
$explanations[$askedQuestions[0]].

$PAR
The answer to the second question is
$correctAns[1], because
$explanations[$askedQuestions[1]].

END_SOLUTION

ENDDOCUMENT();

Answer evaluation and solution: 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