[system] / trunk / pg / macros / contextIntegerFunctions.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

# View of /trunk/pg/macros/contextIntegerFunctions.pl

Tue Jun 21 19:56:00 2005 UTC (14 years, 8 months ago) by jj
File size: 1479 byte(s)
New context which allows students to use C(n,r) and P(n,r) in their
answers.  To support this, the webwork versions of these functions
have been moved from PGaux... to PGcommon...

2
3 sub _contextIntegerFunctions_init {}; # don't reload this file
4
5 ######################################################################
6 #
7 #  This is a Parser context that adds integer related functions C(n,r)
8 #  and P(n,r).  They can be used by the problem author and also by
9 #  students if the answer checking is done by Parser.  The latter is
10 #  the main purpose of this file.
11 #
12 #  Note: by default, webwork problems do not permit students to use
13 #        C(n,r) and P(n,r) functions.  Problems which do permit this
14 #        should alert the student in their text.
15 #
16 #  Usage examples:
17 #     \$b = random(2, 5); \$a = \$b+random(0, 5);
18 #     \$c = C(\$a, \$b);
19 #     ANS(Compute("P(\$a, \$b)")->cmp);
20 #
21 #  Note: If the context is set to something else, such as Numeric, it
22 #        can be set back with Context("IntegerFunctions").
23
24
25 \$context{IntegerFunctions} = Context("Numeric")->copy;
26
27 package IntegerFunction2;
28 our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs
29
30 sub C {
31   shift; my (\$n,\$r) = @_; my \$C = 1;
32   return (0) if(\$r>\$n);
33   \$r = \$n-\$r if (\$r > \$n-\$r); # find the smaller of the two
34   for (1..\$r) {\$C = (\$C*(\$n-\$_+1))/\$_}
35   return \$C
36 }
37
38 sub P {
39   shift; my (\$n,\$r) = @_; my \$P = 1;
40   return (0) if(\$r>\$n);
41   for (1..\$r) {\$P *= (\$n-\$_+1)}
42   return \$P
43 }
44
45 package main;
46