[system] / trunk / pg / macros / contextIntegerFunctions.pl Repository:
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5045 - (download) (as text) (annotate)
Thu Jun 28 01:55:03 2007 UTC (12 years, 7 months ago) by dpvc
File size: 1537 byte(s)
Updated to use the new context copy method.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9