## PREP 2015 Question Authoring - Archived

by Abra Brisbin -
Number of replies: 6
I'm trying to write a problem where the correct answer would be a list of numbers, in which order matters.  I would prefer to have a single answer box, because I don't want to "give away" how many numbers should be in the list.

I've found several ways to have the correct answer be a list of numbers in which order doesn't matter; see below for one such way.  Does anybody have any suggestions?

Thanks!

#---------------------------------------------------------------------------------------------------------------------
# tagging and description section
#---------------------------------------------------------------------------------------------------------------------

# DESCRIPTION
# Predicting the results of "else if" structures.
# This code currently does not do what I want it to;  the answer "10, 4, 16" is counted as correct when it should not be.
# ENDDESCRIPTION
# Library tagging would go here

#---------------------------------------------------------------------------------------------------------------------
# initialization section
#---------------------------------------------------------------------------------------------------------------------

DOCUMENT();
"MathObjects.pl",
"PGML.pl");
Context("Numeric");

#---------------------------------------------------------------------------------------------------------------------
# problem set-up section
#---------------------------------------------------------------------------------------------------------------------
# After I get the answer-checking to take order into account,
# I will change these to be random numbers.
$a = 10;$b = 16;
$c = 4;$ans = '10,16,4';
#$ans = join(',',$a, $b,$c);

#---------------------------------------------------------------------------------------------------------------------
# text section
#---------------------------------------------------------------------------------------------------------------------

TEXT(beginproblem());  # this adds generic problem text (e.g., # of points)
# anything between BEGIN and END PGML is what students see
BEGIN_PGML
This code currently does not do what I want it to;  the answer "10, 4, 16" is counted as correct when it should not be.

Predict the output from the following code.  If there are multiple outputs, separate them by commas.  If the code will produce an error message, type “Error” (without the quotes).

numbers = c( 6, 13, 16 )
for( x in numbers ){
if(x %% 2 == 1){
x = x+3
}
else if(x < 10){
x = 10
}
else{
x = x/4
}
print(x)
}

[__________]{"(10,16,4)"}

END_PGML

#---------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------

BEGIN_PGML_SOLUTION
*SOLUTION*

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: Ordered lists as answers

by Paul Pearson -
Hi Abra,

You will want to create a MathObject List object and then pass "ordered => 1" as an argument to the ->cmp() method that compares the student answer to the correct answer.  For instance, you could replace

[___________]{"(10,16,4)"}

with

[___________]{List(10,16,4)->cmp(ordered=>1)}

in your example.  In the code below my signature, I have opted to define the list as $answer = List(10,16,4); in the problem set up section and then use$answer->cmp(ordered=>1) in the answer checker instead.

Did you intend to have the R code in the text section displayed to students as it currently is?

Best regards,

Paul Pearson

#---------------------------------------------------------------------------------------------------------------------
# tagging and description section
#---------------------------------------------------------------------------------------------------------------------

# DESCRIPTION
# Predicting the results of "else if" structures.
# This code currently does not do what I want it to;  the answer "10, 4, 16" is counted as correct when it should not be.
# ENDDESCRIPTION
# Library tagging would go here

#---------------------------------------------------------------------------------------------------------------------
# initialization section
#---------------------------------------------------------------------------------------------------------------------

DOCUMENT();
"MathObjects.pl",
"PGML.pl");
Context("Numeric");

#---------------------------------------------------------------------------------------------------------------------
# problem set-up section
#---------------------------------------------------------------------------------------------------------------------
# After I get the answer-checking to take order into account,
# I will change these to be random numbers.
$a = 10;$b = 16;
$c = 4;$ans = '10,16,4';
#$ans = join(',',$a, $b,$c);

$answer = List(10,16,4); #--------------------------------------------------------------------------------------------------------------------- # text section #--------------------------------------------------------------------------------------------------------------------- TEXT(beginproblem()); # this adds generic problem text (e.g., # of points) # anything between BEGIN and END PGML is what students see BEGIN_PGML This code currently does not do what I want it to; the answer "10, 4, 16" is counted as correct when it should not be. Predict the output from the following code. If there are multiple outputs, separate them by commas. If the code will produce an error message, type “Error” (without the quotes). numbers = c( 6, 13, 16 ) for( x in numbers ){ if(x %% 2 == 1){ x = x+3 } else if(x < 10){ x = 10 } else{ x = x/4 } print(x) } [__________]{$answer->cmp(ordered=>1)}

END_PGML

#---------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------

BEGIN_PGML_SOLUTION
*SOLUTION*

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: Ordered lists as answers

by Davide Cervone -
I see Paul has already responded, but I did want to say a couple of things more. First, it is great that you are working on a problem that is a sophisticated one, and are pushing the boundaries of what you know how to do. Kudos!

On the other hand, we still have a lot to cover, and some of the ideas needed, here, are ones we haven't gotten to yet. Most of it will come on Monday, as they involve MathObjects (as Paul's answer suggests).

The main thing you need to know is how to create the MathObject by hand so that you can manipulate its answer checker before passing it to PGML. (So far, we have only learned how to pass PGML a string containing the answer, but there are more advanced techniques). Paul's solution is the right one, though there are several possible variations on it. I give one below.

I also had the same question as Paul did about the formatting of the code. We have not yet learned all there is about formatting in PGML, and there is a means of getting pre-formatted output that would handle code listings a bit better than what you currently have. I include it below, and we will see it on Monday, or perhaps next Monday depending on how much progress we make this week.

Finally, I wondered whether you wanted the students to have to type the parentheses around their list or not (since these aren't part of the output). If you do, then put them in the answer, otherwise leave them off, so the student can just type 10,16,4 without parentheses.

Here is a non-randomized version of the core of your problem, with the formatting adjusted, and an alternative method of getting the answer checker with ordered set to true.

Context("Numeric");

$L = Compute("10,16,4");$cmp = $L->cmp(ordered=>1); BEGIN_PGML Predict the output from the following code. If there are multiple outputs, separate them by commas. If the code will produce an error message, type "Error" (without the quotes). : numbers = c(6, 13, 16) : for (x in numbers) { : if (x %% 2 == 1) { : x = x+3 : } : else if (x < 10) { : x = 10 : } : else { : x = x/4 : } : print(x) : } Output = [__________]{$cmp}

END_PGML

I will leave the randomization to you to add.

Note that there is an additional item you will need to consider, which is the fact that if the student types Error, this will produce an error message, not just mark the answer incorrect. So you will need to do a little more in order to allow Error as a possible answer. We will learn how to do that on Monday, so I'm leaving it as something you can add later.

### Re: Ordered lists as answers

by Abra Brisbin -
Ah, thank you, Paul and Davide!

I got the indentations of the R code to look the way I want them in WeBWorK, but for some reason the formatting didn't copy over perfectly into my post.

I'm intrigued to learn more about how to allow "Error" as an incorrect answer!  Unfortunately, I will have to watch the recording of Monday's session, as the live version conflicts with a different professional development workshop I'm committed to attend.

### Re: Ordered lists as answers

by Valerio De Angelis -
I am also interested in having an ordered list of numbers as answer, and this exchange was quite useful. I do have an additional question:

David wrote:
"Finally, I wondered whether you wanted the students to have to type the parentheses around their list or not (since these aren't part of the output)."

I would like the students to know that they have to use the parentheses when the answer is an ordered pair, so the parentheses should be part of the answer, and the system should mark the answer wrong if they are omitted. I tried, but I have not been able to do that.

Thank you,
Valerio

In reply to Valerio De Angelis

### Re: Ordered lists as answers

by Davide Cervone -
If you are using "ordered pairs", these are also commonly called "points", and so you might want to use the Point context. E.g.
Context("Point");

$p = Compute("(2,5)"); BEGIN_PGML The ordered pair is [__________]{$p}
END_PGML

You can also create a point as
\$p = Point(2,5);