File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.25: download - view: text, annotated - select for diffs
Thu May 31 22:36:18 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- cleaning up some garbage

    1: # file name (temp): default_homework
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: #
    4: # Guy Albertelli
    5: #
    6: # 05/25/2001 H. K. Ng
    7: # 05/31/2001 H. K. Ng
    8: #
    9: #init some globals
   10: $RANDOMINIT=0;
   11: $pi=atan2(1,1)*4;
   12: $rad2deg=180.0/$pi;
   13: $deg2rad=$pi/180.0;
   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 var_in_tex {
  126:   if ( $external::target eq "tex" ) {
  127:     return @_[0];
  128:   } else {
  129:     return "";
  130:   }
  131: }
  132: 
  133: sub web {
  134:   if ( $external::target eq "tex" ) {
  135:     return @_[1];
  136:   } else {
  137:     if ( $external::target eq "web") {
  138:       return @_[0];
  139:     } else {
  140:       return @_[2];
  141:     }
  142:   }
  143: }
  144: 
  145: sub html {
  146:   if ( $external::target eq "web" ) {
  147:     return @_[0];
  148:   }
  149: }
  150: 
  151: sub problem {
  152:   return '1';
  153: }
  154: 
  155: sub hinton {
  156:   return 0;
  157: }
  158: 
  159: sub random {
  160:   my ($start,$end,$step)=@_;
  161:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  162:   my $num=1+int(($end-$start)/$step);
  163:   my $result=$start + int(rand() * $num)*$step;
  164:   return $result;
  165: }
  166: 
  167: sub abs { abs(shift) }
  168: sub sin { sin(shift) }
  169: sub cos { cos(shift) }
  170: sub exp { exp(shift) }
  171: sub int { int(shift) }
  172: sub log { log(shift) }
  173: sub atan2 { atan2($_[0],$_[1]) }
  174: sub sqrt { sqrt(shift) }
  175: 
  176: sub tan  { sin($_[0]) / cos($_[0]) }
  177: #sub atan { atan2($_[0], 1); }
  178: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  179: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  180: 
  181: sub log10 { log($_[0])/log(10); }
  182: 
  183: sub factorial {
  184:     my $input = int(shift);
  185:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  186:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  187:     return 1 if $input == 0;
  188:     my $result = 1; 
  189:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  190:     return $result;
  191: }
  192: 
  193: sub sgn {
  194:     return -1 if $_[0] < 0;
  195:     return 0 if $_[0] == 0;
  196:     return 1 if $_[0] > 0;
  197: }
  198: 
  199: sub min {
  200:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  201:     return shift @sorted;
  202: }
  203: 
  204: sub max {
  205:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  206:     return pop @sorted;
  207: }
  208: 
  209: sub roundto {
  210:     my ($input,$n) = @_;
  211:     return sprintf('%.'.$n.'f',$input);
  212: }
  213: 
  214: sub to_string {
  215:     my ($input,$n) = @_;
  216:     return sprintf('%'.$n,$input) if $n ne "";
  217:     return sprintf($input) if $n eq "";
  218: }
  219: 
  220: sub sub_string {
  221:     my ($str,$start,$len) = @_;
  222:     return substr($str,$start-1,$len);
  223: }
  224: 
  225: sub pow   {return $_[0] ** $_[1]; }
  226: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
  227: sub floor {return int($_[0]);  }
  228: 
  229: sub format {
  230:   my ($value,$fmt)=@_;
  231:   return sprintf('%.'.$fmt,$value);
  232: }
  233: 
  234: sub map {
  235:   my $num = $#_/2;
  236:   my $i;
  237:   my @used;
  238:   my @permutation;
  239:   for ($i=0; $i < $num;) {
  240:     $rand = &random(1,$num,1);
  241:     if ( $used[$rand] == '0' ) {
  242:       $i++;
  243:       $used[$rand]='1';
  244:       $permutation[$i]=$rand;
  245:     }
  246:   }
  247:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
  248:     ${$_[$permutation[$i]]}=$_[$i+$num]
  249:   }
  250: }
  251: 
  252: sub capa_id { return }
  253: 
  254: sub problem { return }
  255: 
  256: sub name{
  257:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
  258:   $fullname = "" if $fullname eq ",  ";
  259:   $fullname =~ s/\%2d/-/;
  260:   return $fullname;
  261: }
  262: 
  263: sub student_number { 
  264:   my $id = &EXT('environment.id');
  265:   $id = '' if $id eq "";
  266:   return $id;
  267: }
  268: 
  269: sub class {
  270:   my $course = &EXT('course.description');
  271:   $course = '' if $course eq "";
  272:   return $course;
  273: }
  274: 
  275: sub sec { 
  276:   my $sec = &EXT('request.course.sec');
  277:   $sec = '' if $sec eq "";
  278:   return $sec;
  279: }
  280: 
  281: sub open_date { 
  282:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
  283:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  284:   my @hm = split(/:/,$dc[3]);
  285:   my $ampm = " am";
  286:   if ($hm[0] > 12) {
  287:     $hm[0]-=12;
  288:     $ampm = " pm";
  289:   }
  290:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  291: }
  292: 
  293: sub due_date { 
  294:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
  295:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  296:   my @hm = split(/:/,$dc[3]);
  297:   my $ampm = " am";
  298:   if ($hm[0] > 12) {
  299:     $hm[0]-=12;
  300:     $ampm = " pm";
  301:   }
  302:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  303: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  304: }
  305: 
  306: sub answer_date { 
  307:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
  308:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  309:   my @hm = split(/:/,$dc[3]);
  310:   my $ampm = " am";
  311:   if ($hm[0] > 12) {
  312:     $hm[0]-=12;
  313:     $ampm = " pm";
  314:   }
  315:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  316: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  317: }
  318: 
  319: sub array_moments {
  320:   my @input=@_;
  321:   my (@output,$N);
  322:   $N=scalar (@input);
  323:   $output[0]=$N;
  324:   if ($N <= 1) {
  325:     $output[1]=$input[0];
  326:     $output[1] = "Input array not defined" if ($N == 0);
  327:     $output[2]="variance undefined for N<=1";
  328:     $output[3]="skewness undefined for N<=1";
  329:     $output[4]="kurtosis undefined for N<=1";
  330:     return @output;
  331:   }
  332:   my $sum=0;
  333:   foreach my $line (@input) {
  334:     $sum+=$line;
  335:   }
  336:   $output[1] = $sum/$N;
  337:   my ($x,$sdev,$var,$skew,$kurt) = 0;
  338:   foreach my $line (@input) {
  339:     $x=$line-$output[1];
  340:     $var+=$x**2;
  341:     $skew+=$x**3;
  342:     $kurt+=$x**4;
  343:   }
  344:   $output[2]=$var/($N-1);
  345:   $sdev=sqrt($output[2]);
  346:   if ($sdev == 0) {
  347:      $output[3]="inf-variance=0";
  348:      $output[4]="inf-variance=0";
  349:      return @output;
  350:   }
  351:   $output[3]=$skew/($sdev**3*$N);
  352:   $output[4]=$kurt/($sdev**4*$N)-3;
  353:   return @output;
  354: }
  355: 
  356: sub choose {
  357:   my $num = $_[0];
  358:   return $_[$num];
  359: }
  360: 
  361: 

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