Difference between revisions of "PopUpListsLong"

From WeBWorK_wiki
Jump to navigation Jump to search
m
Line 23: Line 23:
 
loadMacros("PGbasicmacros.pl",
 
loadMacros("PGbasicmacros.pl",
 
"PGchoicemacros.pl",
 
"PGchoicemacros.pl",
"PGanswermacros.pl",
+
"PGanswermacros.pl"
"PGcourse.pl",
 
 
);
 
);
 
TEXT(beginproblem);
 
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ccffcc;padding:7px;">
 
<td style="background-color:#ccffcc;padding:7px;">
 
<p>
 
<p>
Initialization: The first three macros are required.
+
Initialization: to use these pop-up lists, three macro files must be included.
 
</p>
 
</p>
 
</td>
 
</td>
Line 38: Line 36:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
# don't show which answers are correct
  +
$showPartialCorrectAnswers=0;
  +
 
# Create and use pop up lists
 
# Create and use pop up lists
 
$tf = new_select_list();
 
$tf = new_select_list();
Line 43: Line 44:
   
 
# Specify choices presented to students
 
# Specify choices presented to students
$tf -> ra_pop_up_list( [ No_answer => "?", T => "True", F => "False"] );
+
$tf->ra_pop_up_list(
  +
[ No_answer => "?", T => "True",
  +
F => "False"] );
   
 
# Questions and answers
 
# Questions and answers
Line 53: Line 54:
 
"All polynomials are differentiable.",
 
"All polynomials are differentiable.",
 
"T",
 
"T",
"All functions with positive derivatives are increasing.",
+
"All functions with positive derivatives " .
  +
"are increasing.",
 
"T",
 
"T",
 
"All compact sets are closed",
 
"All compact sets are closed",
Line 59: Line 60:
 
"All closed sets are compact",
 
"All closed sets are compact",
 
"F",
 
"F",
"All increasing functions have positive deriviatives",
+
"All increasing functions have positive " .
  +
"derivatives",
 
"F",
 
"F",
"All differentiable strictly increasing functions have non-negative derivatives
+
"All differentiable strictly increasing " .
at every point",
+
"functions have non-negative derivatives " .
  +
"at every point",
 
"T",
 
"T",
 
);
 
);
   
# Number of randomly chosen questions to display to students
+
# Number of randomly chosen questions to
$tf ->choose(4);
+
# display to students
  +
$tf->choose(4);
   
 
</pre>
 
</pre>
Line 73: Line 74:
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
Setup: Make a new select list $tf that "contains" the select list object.
 
  +
In the problem set-up section of the file:
Then, change the printing mechanism of the object to
 
  +
</p>
use pop-up list instead of an answer rule. Construct the choices, and
 
  +
<p>
note how the list is constructed [ answer => list element text, answer => list element text ]
 
  +
Note that for this type of problem we probably don't want to show which parts of the problem are correct, lest it become a "multiple guess" problem type. This can be done by specifying $showPartialCorrectAnswers should be false (zero).
What should the pop-up list contain, and what string should it
 
  +
</p>
submit for an answer when selected?
 
  +
<p>
Submit T when the student selects True and F when False.
 
  +
We make a new select list $tf that "contains" the select list object.
  +
Then, the line specifying the "printq" function changes the printing mechanism of the object to
  +
use a pop-up list instead of an answer rule.
  +
</p>
  +
<p>
  +
The line specifying "ra_pop_up_list" gives a list of the choices that are presented for each question. The format of the list reference is
  +
<code>[answer => list_element_text, answer => list_element_text]</code>:
  +
the text that the the pop-up list should contain, and the string it should
  +
submit for an answer when selected. In this example, if the student selects
  +
"True," the pop-up will submit "T," and similarly "F" for "False."
 
The first choice is a blank to make the students select something.
 
The first choice is a blank to make the students select something.
Insert some questions and their answers (note: each entry has to end with a comma).
 
  +
</p>
 
  +
<p>
  +
Next, we specify some questions and their answers, as a comma-separated list of question and answer pairs. At the end, we specify how many of these to display to the students.
  +
</p>
  +
<p>
  +
(Note that we've broken some of these questions into pieces to avoid long lines on this web page, by using string concatenation. In Perl, the period concatenates strings: thus, <code>"a" . "b"</code> is the same as the string <code>ab</code>.)
 
</p>
 
</p>
 
</td>
 
</td>
Line 107: Line 121:
 
<pre>
 
<pre>
 
ANS(str_cmp($tf->ra_correct_ans));
 
ANS(str_cmp($tf->ra_correct_ans));
 
$showPartialCorrectAnswers=0;
 
 
ENDDOCUMENT();
 
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
Line 199: Line 209:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
$numq = scalar(@questions_and_answers)/2; # $numq = number of questions
+
# $numq = number of questions
  +
$numq = scalar(@questions_and_answers)/2;
  +
 
@shuffle = shuffle($numq);
 
@shuffle = shuffle($numq);
 
@abc = (a..z); # or (A..Z) or (1..$numq)
 
@abc = (a..z); # or (A..Z) or (1..$numq)
   
for $qn (0..$numq-1) { # $qn = question number
+
# $qn = question number
$qns = $shuffle[$qn]; # $qns = question number shuffled
+
for $qn (0..$numq-1) {
  +
# $qns = question number shuffled
  +
$qns = $shuffle[$qn];
  +
 
BEGIN_TEXT
 
BEGIN_TEXT
@abc[$qn]. \{ pop_up_list(['?', @choices]) \} $SPACE
 
  +
@abc[$qn].
  +
\{ pop_up_list(['?', @choices]) \}
  +
$SPACE
 
$questions_and_answers[2*$qns+1]
 
$questions_and_answers[2*$qns+1]
 
$BR$BR
 
$BR$BR
 
END_TEXT
 
END_TEXT
  +
 
ANS(str_cmp($questions_and_answers[2*$qns],
 
ANS(str_cmp($questions_and_answers[2*$qns],
 
filters=>["trim_whitespace","compress_whitespace"]));
 
filters=>["trim_whitespace","compress_whitespace"]));

Revision as of 13:58, 29 October 2009

Pop-Up Lists For Many Questions With Common Answers

Often you may have many questions that have the same set of answers, such as a list of true or false questions, or a list of multiple choice questions in which the choices are the same for each question. This PG code allows takes the hassle out of creating such a PG problem by requiring only that you modify an array of questions and answers, the instructions for the student, and the list of choices for every question. To prevent students from being able to copy each other's answers verbatim, we shuffle the order of the questions. It is also possible to include random elements into each question (but not each answer, since the list of answers is a list of strings and needs to be the same for all questions).

This code shows how to create lengthy pop-up lists in a WeBWorK problem using newer constructs. This example comes from /Library/Rochester/setMAAtutorial/popuplistexample.pg and you are also encouraged to look at Library/Rochester/setMAAtutorial/prob4.pg

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

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

Initialization: to use these pop-up lists, three macro files must be included.

# don't show which answers are correct
$showPartialCorrectAnswers=0;

# Create and use pop up lists 
$tf = new_select_list();
$tf->rf_print_q(~~&pop_up_list_print_q);

# Specify choices presented to students
$tf->ra_pop_up_list( 
    [ No_answer => "?", T => "True", 
      F => "False"] );

# Questions and answers
$tf -> qa ( 
"All continuous functions are differentiable.",
"F",
"All differentiable functions are continuous.",
"T",
"All polynomials are differentiable.",
"T",
"All functions with positive derivatives " .
    "are increasing.",
"T",
"All compact sets are closed",
"T",
"All closed sets are compact",
"F",
"All increasing functions have positive " .
    "derivatives",
"F",
"All differentiable strictly increasing " .
    "functions have non-negative derivatives " .
    "at every point",
"T",
);

# Number of randomly chosen questions to 
#    display to students
$tf->choose(4);

In the problem set-up section of the file:

Note that for this type of problem we probably don't want to show which parts of the problem are correct, lest it become a "multiple guess" problem type. This can be done by specifying $showPartialCorrectAnswers should be false (zero).

We make a new select list $tf that "contains" the select list object. Then, the line specifying the "printq" function changes the printing mechanism of the object to use a pop-up list instead of an answer rule.

The line specifying "ra_pop_up_list" gives a list of the choices that are presented for each question. The format of the list reference is [answer => list_element_text, answer => list_element_text]: the text that the the pop-up list should contain, and the string it should submit for an answer when selected. In this example, if the student selects "True," the pop-up will submit "T," and similarly "F" for "False." The first choice is a blank to make the students select something.

Next, we specify some questions and their answers, as a comma-separated list of question and answer pairs. At the end, we specify how many of these to display to the students.

(Note that we've broken some of these questions into pieces to avoid long lines on this web page, by using string concatenation. In Perl, the period concatenates strings: thus, "a" . "b" is the same as the string ab.)

BEGIN_TEXT

Are the following statements true or false? 
$BR
\{ $tf-> print_q \}

END_TEXT

Instructions and Question for all parts: You may modify the instructions if you want. You should explicitly say what all of the possible answers are so that students who have a pdf or a paper copy can work directly from it.

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

Main Text and Answer Evaluation: You should not need to modify this section at all.



This code shows how to create lengthy pop-up lists in a WeBWorK problem using older-style answer checkers, some deprecated elements such as shuffle, and elementary bits of code to build things from scratch.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
  "PG.pl",
  "PGstandard.pl",
  "PGcourse.pl",
  "PGchoicemacros.pl",
);

TEXT(beginproblem);

Initialization: We need to include the PGchoicemacros.pl macro file.

$a = random(1,5,1);

@questions_and_answers = (
"True",  "Larry is one of the three stooges.",
"False", "Gandalf is one of the three stooges.",
"False", "\(\pi + $a\) is rational.",
);

@choices = ("True", "False");

Set-up: Create an array in which questions alternate with answers. Answers may be any string. For true or false questions, this is the only part of the example that needs to be modified. We added a randomized element to the question about pi. Do not use the names $numq, $qn, or $qns to avoid conflicts with the code below.

BEGIN_TEXT

Are the following statements true or false? 
$BR$BR

END_TEXT

Instructions and Question for all parts: You may modify the instructions if you want. You should explicitly say what all of the possible answers are so that students who have a pdf or a paper copy can work directly from it.

# $numq = number of questions
$numq = scalar(@questions_and_answers)/2;

@shuffle = shuffle($numq);
@abc = (a..z); # or (A..Z) or (1..$numq)

# $qn  = question number
for $qn (0..$numq-1) { 
# $qns = question number shuffled
$qns = $shuffle[$qn];  

BEGIN_TEXT
@abc[$qn]. 
\{ pop_up_list(['?', @choices]) \}
$SPACE 
$questions_and_answers[2*$qns+1]
$BR$BR
END_TEXT

ANS(str_cmp($questions_and_answers[2*$qns], 
filters=>["trim_whitespace","compress_whitespace"]));
}

$showPartialCorrectAnswers=0;

ENDDOCUMENT(); 

Main Text and Answer Evaluation: You should not need to modify this section at all. We shuffle the order of the questions and use a for loop to generate one question and one answer evaluator for each pair in the array @questions_and_answers.

Problem Techniques Index