WeBWorK Problems

Allowing P and C in student answers

Allowing P and C in student answers

by Christopher Heckman -
Number of replies: 6
I teach a Discrete Mathematics class, and part of the course is devoted to basic counting techniques. There are lots of problems written for this, but students are not allowed to use the P (permutation) and C (combination) functions in their answers.

Now, I am not so picky, once they know what these functions are, and so I set about rewriting some of the problems to allow P(n,r) and C(n,r). However, I get stuck in one of two places.

Code similar to the following should work:

DOCUMENT();

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

Context ("Numeric");
Context () -> functions -> enable ('P');

TEXT(beginproblem());

$pi = Real("pi");
Context()->texStrings;

BEGIN_TEXT
Enter a value for \(\pi\)
\{ $pi -> ans_rule \}
END_TEXT

Context()->normalStrings;

ANS($pi -> cmp);

ENDDOCUMENT();

However, it doesn't. The WeBWorK documentation mentions the Context() line and gives examples where trig functions (and so on) are enabled or disabled. The Available Functions page mentioned the PGauxiliaryFunctions.pl file. In short, this should work.

I've tried various omissions, rearrangements, and so on. I either get a message that P is undefined, or I get the cryptic message:

Can't locate object method "weaken" via package "main"

which suggests that contextIntegerFunctions.pl needs to be rewritten.

I've also checked online and in the Problem Library, and NO ONE has written a problem that allows the students to use the P and C functions. So that leads me here.

What's the fix?

--- Chris
In reply to Christopher Heckman

Re: Allowing P and C in student answers

by Alex Jordan -
I can either enter "P(4,2)" or "12" and this works out. Can you modify htis to what you need?

Basically you need to be in IntegerFunctions context, not Numeric. And you do not need to enable P. Although I am not sure why trying to enable it leads to the error.


DOCUMENT();

loadMacros(
"PGbasicmacros.pl",
"MathObjects.pl",
"contextIntegerFunctions.pl",
);

Context ("IntegerFunctions");

TEXT(beginproblem());

$p = Compute("P(4,2)");
Context()->texStrings;

BEGIN_TEXT
Enter a value for \(P(4,2)\)
\{ $p -> ans_rule \}
END_TEXT

Context()->normalStrings;

ANS($p -> cmp);

ENDDOCUMENT();

In reply to Alex Jordan

Re: Allowing P and C in student answers

by Christopher Heckman -
Yes, that works for me, too. I guess I never bothered trying the problem where I deleted the enable statement. So is the Wiki wrong, or is it just out of date?

Also, you don't need Context ("IntegerFunctions") if the last file you load is contextIntegerFunctions.pl. It's probably good practice, though.

Thanks for the quick reply!

ADDITIONAL: You also need to use MathObjects, so don't use std_num_cmp or any of the old evaluators.
In reply to Christopher Heckman

Re: Allowing P and C in student answers

by Alex Jordan -
Can you point to the wiki page with bad information? I should be able to fix it.
In reply to Alex Jordan

Re: Allowing P and C in student answers

by Christopher Heckman -
I was planning on adding a template at the Wiki when I get done with updating my problems. Earlier today, I changed the wiki pages. (One was the Available Functions page, and the other was Specialized Contexts.)
In reply to Christopher Heckman

Re: Allowing P and C in student answers

by Christopher Heckman -
On second thought ... It looks like writing up a sample problem is more work than I intended, so you can do it if you want. Here's the sample problem I want to use:

DOCUMENT();

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl",
"contextIntegerFunctions.pl",
);

TEXT(&beginproblem);

$a = non_zero_random(-6,6,2);
$b = non_zero_random(-6,6,2);
$c = random(3,7,1);
$d = random(10,15,1);
$e = $c+$d;

$ans = $a**$c*$b**$d*C($e, $d);

BEGIN_TEXT
What is the coefficient of \( x^{$c} y^{$d} \) in the expansion of
\( ($a x+$b y)^{$e} \)? $BR
\{ ans_rule(30) \}
$PAR
END_TEXT

ANS( Real( $ans ) -> cmp() );

ENDDOCUMENT();

Some comments:
  1. If contextIntegerFunctions.pl is not the last macro file you load, you need to include the command Context ("IntegerFunctions");
  2. You do not need to (and should not) use the command Context() -> functions -> enable ("P,C")
  3. MathObjects are (evidently) required, even if the answer is just a single number. That means you either have to use $answer = Real (expression) and ANS($answer -> cmp()) or short-cut it with ANS(Real($answer) -> cmp()); Note that ANS(std_num_cmp($answer)) will not work! (I found this out the hard way.)
In reply to Christopher Heckman

Re: Allowing P and C in student answers

by Alex Jordan -
It looks like maybe you are modeling from some outdated templates. I thought it might be worth offering how I might write this problem today:

DOCUMENT();

loadMacros(
"PGstandard.pl", #loads four macros you loaded
"MathObjects.pl", #almost always best practice
"contextIntegerFunctions.pl",
"PGML.pl", #my preference; easier for
#future users to work with
"PGcourse.pl", #allow yourself and future
#users to make course-wide
#customizations by editing this
);

TEXT(beginproblem()); #somewhere I heard it's better
#to cease using & and use ()
#instead; can't recall why

$a = non_zero_random(-6,6,2);
$b = non_zero_random(-6,6,2);
$c = random(3,7);
$d = random(10,15);
$e = $c+$d;

Context("IntegerFunctions");

$ans = Real("($a)^($c) ($b)^($d) C($e,$d)");
#makes $ans a MathObject Real, which among other
#things will enable special feedback messages to
#certain incorrect answers automatically;
#by using quotes, you pass a string instead of a
#perl real, and you can use ^ instead of **;
#the parens are needed in the bases, probably
#extraneous for the exponents

Context()->variables->add(y=>'Real');
$f=Formula("($a x + $b y)^($e)")->reduce;
#in case $a or $b is -1, 0, or 1; which is not #possible with your randomization, but this
#is generally helpful
$term=Formula("x^($c)y^($d)")->reduce;

#PGML feels cleaner to me and easier for a future
#person to work with; also you get the answer
#evaluation right next to the answer blank

BEGIN_PGML
What is the coefficient of [`[$term]`] in the expansion of [`[$f]`]?

[_____________]{$ans}

END_PGML

ENDDOCUMENT();