File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.20: download - view: text, annotated - select for diffs
Fri May 25 21:20:19 2001 UTC (22 years, 11 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
add the following functions: min, max, factorial, roundto, to_string, sub_string

    1: # file name (temp): default_homework 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: # 
    4: #
    5: #init some globals
    6: #use Math::Trig;
    7: #
    8: # Guy Albertelli
    9: #
   10: # 05/25/2001 H. K. Ng
   11: #
   12: $RANDOMINIT=0;
   13: $pi=atan2(1,1)*4;
   14: 
   15: sub caparesponse_check {
   16:   my $answer='';  #done
   17:   my $type='';    #done
   18:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   19:   my $tol='';     #done
   20:   my $sig='';     #done lowerbnd,upperbnd
   21:   my $sig_lbound=''; #done
   22:   my $sig_ubound=''; #done
   23:   my $ans_fmt='';
   24:   my $units='';     #done
   25:   my $calc='';
   26:   my ($response,$expr)=@_;
   27:   
   28:   ($answer,$type,$tol,$sig,$ans_fmt,
   29:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
   30:   #type's definitons come from capaParser.h
   31:   if ($type eq '' ) {
   32:     if ( $answer eq ($answer *1.0)) { $type = 2;
   33:     } else { $type = 3; }
   34:   } else {
   35:          if ($type eq 'cs')    { $type = 4;
   36:     } elsif ($type eq 'ci')    { $type = 3;
   37:     } elsif ($type eq 'mc')    { $type = 5;
   38:     } elsif ($type eq 'fml')   { $type = 8;
   39:     } elsif ($type eq 'subj')  { $type = 7;
   40:     } else { return "ERROR: Unknown type of answer: $type" }
   41:   }
   42: 
   43:   if ($tol eq '') {
   44:     $tol=0.0;
   45:     $tol_type=1; #TOL_ABSOLUTE
   46:   } else {
   47:     if ($tol =~ /%$/) {
   48:       chop $tol;
   49:       $tol_type=2; #TOL_PERCENTAGE
   50:     } else {
   51:       $tol_type=1; #TOL_ABSOLUTE
   52:     }
   53:   }
   54: 
   55:   if ($sig eq '') {
   56:     $sig_lbound = 0; #SIG_LB_DEFAULT
   57:     $sig_ubound =15; #SIG_UB_DEFAULT
   58:   } else {
   59:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   60:   }
   61:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   62: 					       $tol_type,$tol,
   63: 					       $sig_lbound,$sig_ubound,
   64: 					       $ans_fmt,$units,$calc);
   65: 
   66:   if    ($result == '1') { $result='EXACT_ANS'; } 
   67:   elsif ($result == '2') { $result='APPROX_ANS'; }
   68:   elsif ($result == '3') { $result='SIG_FAIL'; }
   69:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   70:   elsif ($result == '5') { $result='NO_UNIT'; }
   71:   elsif ($result == '6') { $result='UNIT_OK'; }
   72:   elsif ($result == '7') { $result='INCORRECT'; }
   73:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   74:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   75:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   76:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   77:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   78:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   79: 
   80:   return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br />$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br />";
   81: }
   82: 
   83: sub caparesponse_check_list {
   84:   my ($response,$expr)=@_;
   85:   my $result='';
   86:   my $aresult='';
   87:   my $current_answer;
   88:   my $answer = eval $expr.';return $answer';
   89:   my (@list) = eval $answer;
   90:   my (@responselist)=split /,/,$response;
   91:   my $unit='';
   92:   $result.="Final response :$responselist['-1']:<br />";
   93:   if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
   94:     $responselist['-1']=$1;
   95:     $unit=$2;
   96:   }
   97:   $result.="Final response :$responselist['-1']:<br />";
   98:   $unit=~s/\s//;
   99:   my $i=0;
  100:   my $awards='';
  101:   for ($i=0; $i<@list;$i++) {
  102:     if ($unit eq '') {
  103:       $aresult=&caparesponse_check($responselist[$i],
  104: 			     $expr.';my $answer='.$list[$i].';');
  105:     } else {
  106:       $aresult=&caparesponse_check($responselist[$i]." $unit",
  107: 				   $expr.';my $answer='.$list[$i].';');
  108:     }
  109:     my ($temp)=split /:/, $aresult;
  110:     $awards.="$temp,";
  111:     $result.=$aresult;
  112:   }
  113:   chop $awards;
  114:   return "$awards:<br />$result";
  115: }
  116: 
  117: sub tex {
  118:   if ( $external::target eq "tex" ) {
  119:     return @_[0];
  120:   } else {
  121:     return @_[1];
  122:   }
  123: }
  124: 
  125: sub web {
  126:   if ( $external::target eq "tex" ) {
  127:     return @_[1];
  128:   } else {
  129:     if ( $external::target eq "web") {
  130:       return @_[0];
  131:     } else {
  132:       return @_[2];
  133:     }
  134:   }
  135: }
  136: 
  137: sub problem {
  138:   return '1';
  139: }
  140: 
  141: sub hinton {
  142:   return 0;
  143: }
  144: 
  145: sub random {
  146:   my ($start,$end,$step)=@_;
  147:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  148:   my $num=1+int(($end-$start)/$step);
  149:   my $result=$start + int(rand() * $num)*$step;
  150:   return $result;
  151: }
  152: 
  153: sub tan  { sin($_[0]) / cos($_[0]) }
  154: sub atan { atan2($_[0], 1); }
  155: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  156: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  157: sub log10 { log($_[0])/log(10); }
  158: sub factorial {
  159:     my $input = int(shift);
  160:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  161:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  162:     return 1 if $input == 0;
  163:     my $result = 1; 
  164:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  165:     return $result;
  166: }
  167: 
  168: sub sgn {
  169:     return -1 if $_[0] < 0;
  170:     return 0 if $_[0] == 0;
  171:     return 1 if $_[0] > 0;
  172: }
  173: 
  174: sub min {
  175:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  176:     return shift @sorted;
  177: }
  178: 
  179: sub max {
  180:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  181:     return pop @sorted;
  182: }
  183: 
  184: sub html {
  185:   if ( $external::target eq "web" ) {
  186:     return @_[0];
  187:   }
  188: }
  189: 
  190: sub roundto {
  191:     my ($input,$n) = @_;
  192:     return sprintf('%.'.$n.'f',$input);
  193: }
  194: 
  195: sub to_string {
  196:     my ($input,$n) = @_;
  197:     return sprintf('%'.$n,$input) if $n ne "";
  198:     return sprintf($input) if $n eq "";
  199: }
  200: 
  201: sub sub_string {
  202:     my ($str,$start,$len) = @_;
  203:     return substr($str,$start-1,$len);
  204: }
  205: 
  206: sub pow   {return $_[0] ** $_[1]; }
  207: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
  208: sub floor {return int($_[0]);  }
  209: 
  210: sub format {
  211:   my ($value,$fmt)=@_;
  212:   return sprintf('%.'.$fmt,$value);
  213: }
  214: 
  215: sub map {
  216:   my $num = $#_/2;
  217:   my $i;
  218:   my @used;
  219:   my @permutation;
  220:   for ($i=0; $i < $num;) {
  221:     $rand = &random(1,$num,1);
  222:     if ( $used[$rand] == '0' ) {
  223:       $i++;
  224:       $used[$rand]='1';
  225:       $permutation[$i]=$rand;
  226:     }
  227:   }
  228:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
  229:     ${$_[$permutation[$i]]}=$_[$i+$num]
  230:   }
  231: }
  232: 
  233: sub choose {
  234:   my $num = $_[0];
  235:   return $_[$num];
  236: }
  237: #$external::randomseed=time;
  238: #$a=2;
  239: #$b=2;
  240: #$c=2;
  241: #&map(1,\$a,\$b,\$c,3,4,5);
  242: #print $a."\n".$b."\n";

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>