Annotation of loncom/homework/default_homework.lcpm, revision 1.13

1.1       harris41    1: # file name (temp): default_homework 
                      2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
                      3: # 
                      4: #
1.3       albertel    5: $RANDOMINIT=0;
                      6: 
1.7       albertel    7: sub caparesponse_check {
1.10      albertel    8:   my $answer='';  #done
                      9:   my $type='';    #done
                     10:   my $tol_type=''; # gets it's value from whether tol has a % or not done
                     11:   my $tol='';     #done
1.12      albertel   12:   my $sig='';
1.7       albertel   13:   my $sig_lbound='';
                     14:   my $sig_ubound='';
                     15:   my $ans_fmt='';
1.9       albertel   16:   my $units='';
1.7       albertel   17:   my $calc='';
                     18:   my ($response,$expr)=@_;
                     19:   
1.12      albertel   20:   ($answer,$type,$tol,$sig,$ans_fmt,
                     21:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
1.8       albertel   22:   #type's definitons come from capaParser.h
                     23:   if ($type eq '' ) {
                     24:     if ( $answer eq ($answer *1.0)) { $type = 2;
                     25:     } else { $type = 3; }
                     26:   } else {
                     27:          if ($type eq 'cs')    { $type = 4;
                     28:     } elsif ($type eq 'ci')    { $type = 3;
                     29:     } elsif ($type eq 'mc')    { $type = 5;
                     30:     } elsif ($type eq 'fml')   { $type = 8;
1.9       albertel   31:     } elsif ($type eq 'subj')  { $type = 7;
                     32:     } else { return "ERROR: Unknown type of answer: $type" }
1.8       albertel   33:   }
                     34: 
1.10      albertel   35:   if ($tol eq '') {
                     36:     $tol=0.0;
                     37:     $tol_type=1; #TOL_ABSOLUTE
                     38:   } else {
                     39:     if ($tol =~ /%$/) {
1.12      albertel   40:       chop $tol;
1.10      albertel   41:       $tol_type=2; #TOL_PERCENTAGE
                     42:     } else {
                     43:       $tol_type=1; #TOL_ABSOLUTE
                     44:     }
                     45:   }
1.12      albertel   46: 
                     47:   if ($sig eq '') {
                     48:     $sig_lbound = 0; #SIG_LB_DEFAULT
                     49:     $sig_ubound =15; #SIG_UB_DEFAULT
                     50:   } else {
                     51:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
                     52:   }
1.7       albertel   53:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10      albertel   54: 					       $tol_type,$tol,
1.7       albertel   55: 					       $sig_lbound,$sig_ubound,
1.12      albertel   56: 					       $ans_fmt,$units,$calc);
1.9       albertel   57: 
                     58:   if    ($result == '1') { $result='EXACT_ANS'; } 
                     59:   elsif ($result == '2') { $result='APPROX_ANS'; }
                     60:   elsif ($result == '3') { $result='SIG_FAIL'; }
                     61:   elsif ($result == '4') { $result='UNIT_FAIL'; }
                     62:   elsif ($result == '5') { $result='NO_UNIT'; }
                     63:   elsif ($result == '6') { $result='UNIT_OK'; }
                     64:   elsif ($result == '7') { $result='INCORRECT'; }
                     65:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
                     66:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
                     67:   elsif ($result =='10') { $result='SUB_RECORDED'; }
                     68:   elsif ($result =='11') { $result='BAD_FORMULA'; }
                     69:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.13    ! albertel   70:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
1.9       albertel   71: 
1.12      albertel   72:   return "$result:$error:$answer:$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units";
1.7       albertel   73: }
                     74: 
1.4       albertel   75: sub tex {
                     76:   if ( $external::target eq "tex" ) {
                     77:     return @_[0];
                     78:   } else {
                     79:     return @_[1];
                     80:   }
                     81: }
                     82: 
                     83: sub web {
                     84:   if ( $external::target eq "tex" ) {
                     85:     return @_[1];
                     86:   } else {
                     87:     if ( $external::target eq "web") {
                     88:       return @_[0];
                     89:     } else {
                     90:       return @_[2];
                     91:     }
                     92:   }
                     93: }
                     94: 
                     95: sub problem {
1.11      albertel   96:   return '1';
1.4       albertel   97: }
                     98: 
1.1       harris41   99: sub hinton {
                    100:   return 0;
                    101: }
                    102: 
                    103: sub random {
                    104:   my ($start,$end,$step)=@_;
1.3       albertel  105:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
1.1       harris41  106:   my $num=1+int(($end-$start)/$step);
                    107:   my $result=$start + int(rand() * $num)*$step;
                    108:   return $result;
                    109: }
                    110: 
                    111: sub tan  { sin($_[0]) / cos($_[0]) }
                    112: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
                    113: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
                    114: 
                    115: sub html {
1.4       albertel  116:   if ( $external::target eq "web" ) {
                    117:     return @_[0];
1.1       harris41  118:   }
                    119: }
                    120: 
                    121: 
                    122: sub pow   {return $_[0] ** $_[1]; }
                    123: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
                    124: sub floor {return int($_[0]);  }
                    125: 
1.2       albertel  126: sub format {
                    127:   my ($value,$fmt)=@_;
1.11      albertel  128:   return sprintf('%.'.$fmt,$value);
1.2       albertel  129: }
1.5       albertel  130: 
                    131: sub map {
                    132:   my $num = $#_/2;
                    133:   my $i;
1.6       albertel  134:   my @used;
                    135:   my @permutation;
                    136:   for ($i=0; $i < $num;) {
                    137:     $rand = &random(1,$num,1);
                    138:     if ( $used[$rand] == '0' ) {
                    139:       $i++;
                    140:       $used[$rand]='1';
                    141:       $permutation[$i]=$rand;
                    142:     }
                    143:   }
1.5       albertel  144:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
1.6       albertel  145:     ${$_[$permutation[$i]]}=$_[$i+$num]
1.5       albertel  146:   }
                    147: }
                    148: 
                    149: sub choose {
                    150:   my $num = $_[0];
                    151:   return $_[$num];
                    152: }
1.6       albertel  153: #$external::randomseed=time;
1.5       albertel  154: #$a=2;
                    155: #$b=2;
1.6       albertel  156: #$c=2;
                    157: #&map(1,\$a,\$b,\$c,3,4,5);
1.5       albertel  158: #print $a."\n".$b."\n";

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