--- loncom/homework/default_homework.lcpm 2004/06/09 21:08:21 1.86 +++ loncom/homework/default_homework.lcpm 2005/02/17 23:42:38 1.93 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run() # -# $Id: default_homework.lcpm,v 1.86 2004/06/09 21:08:21 albertel Exp $ +# $Id: default_homework.lcpm,v 1.93 2005/02/17 23:42:38 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -34,6 +34,32 @@ $rad2deg=180.0/$pi; $deg2rad=$pi/180.0; $"=' '; +sub check_commas { + my ($response)=@_; + #print("$response "); + my @numbers=split(',',$response); + #print(" numbers ".join('-',@numbers)." "); + if (scalar(@numbers) > 1) { + #print(" numbers[0] ".$numbers[0]." "); + if (length($numbers[0]) > 3 || length($numbers[0]) == 0) { return -1; } + shift(@numbers); + #print(" numbers ".scalar(@numbers)." "); + while (scalar(@numbers) > 1) { + #print(" numbers ".join('-',@numbers)." "); + if (length($numbers[0]) != 3) { return -2; } + shift(@numbers); + } + my ($number)=split('\.',$numbers[0]); + #print(" number ".$number." "); + #print(" numbers[0] ".$numbers[0]." "); + if (length($number) != 3) { return -3; } + } else { + my ($number)=split('\.',$numbers[0]); + if (length($number) > 3) { return -4; } + } + return 1; +} + sub caparesponse_check { my ($answer,$response)=@_; #not properly used yet: calc @@ -41,7 +67,7 @@ sub caparesponse_check { my $type=$LONCAPA::CAPAresponse_args{'type'}; my $tol=$LONCAPA::CAPAresponse_args{'tol'}; my $sig=$LONCAPA::CAPAresponse_args{'sig'}; - my $ans_fmt=$LONCAPA::CAPAresponse_args{'ans_fmt'}; + my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'}; my $unit=$LONCAPA::CAPAresponse_args{'unit'}; my $calc=$LONCAPA::CAPAresponse_args{'calc'}; my $samples=$LONCAPA::CAPAresponse_args{'samples'}; @@ -69,7 +95,17 @@ sub caparesponse_check { $answer=~s/ +/ /g; $response=~s/ +/ /g; } + if ($type eq 'float' && $unit=~/\$/) { + if ($response!~/^\$/) { return "NO_UNIT: Missing \$ "; } + $response=~s/\$//g; + } + if ($type eq 'float' && $unit=~/\,/ && (&check_commas($response)<0)) { + return "COMMA_FAIL:"; + } + $ans_fmt=~s/\W//g; + $unit=~s/[\$,]//g; if ($type eq 'float') { $response=~s/,//g; } + if (length($response) > 500) { return "TOO_LONG: Answer too long"; } if ($type eq '' ) { @@ -344,7 +380,7 @@ sub random_multivariate_normal { my ($item_cnt,$seed,$mean,$covar) = @_; my @oldseed=&random_get_seed(); &random_set_seed_from_phrase($seed); - @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar); + my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar); &random_set_seed(@oldseed); return @retArray; } @@ -354,7 +390,7 @@ sub random_multinomial { my @oldseed=&random_get_seed(); my @retArray; &random_set_seed_from_phrase($seed); - @retArray=&math_random_multinomial($item_cnt,@p); + my @retArray=&math_random_multinomial($item_cnt,@p); &random_set_seed(@oldseed); return @retArray; } @@ -508,8 +544,8 @@ sub chemparse { } next; } - $token =~ /^\s*(\d*)(.*)/; - $formula .= $1 if ($1 > 1); # stoichiometric coefficient + $token =~ /^\s*(\d*(?:&frac\d\d)?)(.*)/; + $formula .= $1 if ($1 ne '1'); # stoichiometric coefficient my $molecule = $2; # subscripts @@ -523,8 +559,7 @@ sub chemparse { $formula .= $molecule.' '; } # get rid of trailing space - $formula =~ s/(\Q${nbsp}\E| )$//; - + $formula =~ s/(\ \;| )$//; return &xmlparse($formula); }