From: jj v. a. <we...@ma...> - 2005-06-21 19:58:37
|
Log Message: ----------- 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... Modified Files: -------------- pg/macros: PGauxiliaryFunctions.pl PGcommonFunctions.pl Added Files: ----------- pg/macros: contextIntegerFunctions.pl Revision Data ------------- Index: PGauxiliaryFunctions.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/PGauxiliaryFunctions.pl,v retrieving revision 1.8 retrieving revision 1.9 diff -Lmacros/PGauxiliaryFunctions.pl -Lmacros/PGauxiliaryFunctions.pl -u -r1.8 -r1.9 --- macros/PGauxiliaryFunctions.pl +++ macros/PGauxiliaryFunctions.pl @@ -193,44 +193,6 @@ return $num.$obj; } -# Combinations and permutations - -sub C { - my $n = shift; - my $k = shift; - my $ans = 1; - - return(0) if ($k>$n); - if($k>($n-$k)) { $k = $n-$k; } - for (1..$k) { $ans = ($ans*($n-$_+1))/$_; } - return $ans; -} - -sub Comb { - C(@_); -} - -sub P { - my $n = shift; - my $k = shift; - my $perm = 1; - - if($n != int($n) or $n < 0) { - warn 'Non-negative integer required.'; - return; - } - if($k>$n) { - warn 'Second argument of Permutation bigger than first.'; - return; - } - for (($n-$k+1)..$n) { $perm *= $_;} - return $perm; -} - -sub Perm { - P(@_); -} - #factorial sub fact { Index: PGcommonFunctions.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/PGcommonFunctions.pl,v retrieving revision 1.4 retrieving revision 1.5 diff -Lmacros/PGcommonFunctions.pl -Lmacros/PGcommonFunctions.pl -u -r1.4 -r1.5 --- macros/PGcommonFunctions.pl +++ macros/PGcommonFunctions.pl @@ -62,6 +62,23 @@ sub sgn {$_[1] <=> 0} +#our @ISA = qw(Parser::Function::numeric2); +sub C { + shift; my ($n,$r) = @_; my $C = 1; + return (0) if($r>$n); + $r = $n-$r if ($r > $n-$r); # find the smaller of the two + for (1..$r) {$C = ($C*($n-$_+1))/$_} + return $C +} + +sub P { + shift; my ($n,$r) = @_; my $P = 1; + return (0) if($r>$n); + for (1..$r) {$P *= ($n-$_+1)} + return $P +} + + # # Back to main package # @@ -103,4 +120,9 @@ sub sgn {CommonFunction->Call('sgn',@_)} +sub C {CommonFunction->Call('C', @_)} +sub P {CommonFunction->Call('P', @_)} +sub Comb {CommonFunction->Call('C', @_)} +sub Perm {CommonFunction->Call('P', @_)} + 1; --- /dev/null +++ macros/contextIntegerFunctions.pl @@ -0,0 +1,53 @@ +loadMacros('Parser.pl'); + +sub _contextIntegerFunctions_init {}; # don't reload this file + +###################################################################### +# +# This is a Parser context that adds integer related functions C(n,r) +# and P(n,r). They can be used by the problem author and also by +# students if the answer checking is done by Parser. The latter is +# the main purpose of this file. +# +# Note: by default, webwork problems do not permit students to use +# C(n,r) and P(n,r) functions. Problems which do permit this +# should alert the student in their text. +# +# Usage examples: +# $b = random(2, 5); $a = $b+random(0, 5); +# $c = C($a, $b); +# ANS(Compute("P($a, $b)")->cmp); +# +# Note: If the context is set to something else, such as Numeric, it +# can be set back with Context("IntegerFunctions"). + + +$context{IntegerFunctions} = Context("Numeric")->copy; + +package IntegerFunction2; +our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs + +sub C { + shift; my ($n,$r) = @_; my $C = 1; + return (0) if($r>$n); + $r = $n-$r if ($r > $n-$r); # find the smaller of the two + for (1..$r) {$C = ($C*($n-$_+1))/$_} + return $C +} + +sub P { + shift; my ($n,$r) = @_; my $P = 1; + return (0) if($r>$n); + for (1..$r) {$P *= ($n-$_+1)} + return $P +} + +package main; + +$context{'IntegerFunctions'}->functions->add( + C => {class => 'IntegerFunction2'}, + P => {class => 'IntegerFunction2'}, +); + +Context("IntegerFunctions"); + |