File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.40: download - view: text, annotated - select for diffs
Tue Nov 27 14:57:54 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
- bug fix, @list was improperly scoped to just the if
- get_array_args, passes back messages and a list of results

    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: $hidden::RANDOMINIT=0;
   11: $pi=atan2(1,1)*4;
   12: $rad2deg=180.0/$pi;
   13: $deg2rad=$pi/180.0;
   14: 
   15: sub caparesponse_check {
   16:   #not properly used yet: calc
   17:   #not to be used: $ans_fmt
   18:   my ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
   19:     eval $_[1].
   20:       ';return ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples);';
   21: 
   22:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   23:   my $sig_lbound=''; #done
   24:   my $sig_ubound=''; #done
   25:   my ($response,$expr)=@_;
   26: 
   27: 
   28:   #type's definitons come from capaParser.h
   29:   my $message='';
   30:   #remove leading and trailing whitespace
   31:   if ($response=~ /^\s|\s$/) {
   32:     $response=~ s:^\s+|\s+$::g;
   33:     $message .="Removed ws now :$response:<br />";
   34:   } else {
   35:     $message .="no ws in :$response:<br />";
   36:   }
   37: 
   38:   if ($type eq '' ) {
   39:     $message .= "Didn't find a type :$type:$expr: defaulting<br />";
   40:     if ( $answer eq ($answer *1.0)) { $type = 2;
   41:     } else { $type = 3; }
   42:   } else {
   43:          if ($type eq 'cs')    { $type = 4;
   44:     } elsif ($type eq 'ci')    { $type = 3;
   45:     } elsif ($type eq 'mc')    { $type = 5;
   46:     } elsif ($type eq 'fml')   { $type = 8;
   47:     } elsif ($type eq 'subj')  { $type = 7;
   48:     } else { return "ERROR: Unknown type of answer: $type" }
   49:   }
   50: 
   51:   my $points;
   52:   my $id_list;
   53:   #formula type setup the sample points
   54:   if ($type eq '8') {
   55:     ($id_list,$points)=split(/@/,$samples);
   56:     $message.="Found :$points: points<br />";
   57:   }
   58:   if ($tol eq '') {
   59:     $tol=0.0;
   60:     $tol_type=1; #TOL_ABSOLUTE
   61:   } else {
   62:     if ($tol =~ /%$/) {
   63:       chop $tol;
   64:       $tol_type=2; #TOL_PERCENTAGE
   65:     } else {
   66:       $tol_type=1; #TOL_ABSOLUTE
   67:     }
   68:   }
   69: 
   70:   if ($sig eq '') {
   71:     $sig_lbound = 0; #SIG_LB_DEFAULT
   72:     $sig_ubound =15; #SIG_UB_DEFAULT
   73:   } else {
   74:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   75:   }
   76:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   77: 					       $tol_type,$tol,
   78: 					       $sig_lbound,$sig_ubound,
   79: 					       $ans_fmt,$unit,$calc,$id_list,
   80: 					       $points,$external::randomseed);
   81: 
   82:   if    ($result == '1') { $result='EXACT_ANS'; } 
   83:   elsif ($result == '2') { $result='APPROX_ANS'; }
   84:   elsif ($result == '3') { $result='SIG_FAIL'; }
   85:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   86:   elsif ($result == '5') { $result='NO_UNIT'; }
   87:   elsif ($result == '6') { $result='UNIT_OK'; }
   88:   elsif ($result == '7') { $result='INCORRECT'; }
   89:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   90:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   91:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   92:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   93:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   94:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   95: 
   96:   return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br /> type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|<br />$message$expr";
   97: }
   98: 
   99: sub get_array_args {
  100:   my ($expr,$arg)=@_;
  101:   # do these first, because who knows what varname the instructor might have used
  102:   # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
  103:   my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #'
  104:   my $GET_ARRAY_ARGS_result;
  105:   my @GET_ARRAY_ARGS_list;
  106:   if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
  107:     (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
  108:   }
  109:   $GET_ARRAY_ARGS_result.="error:$@:<br />";
  110:   # if the eval fails just use what is in the answer exactly
  111:   if (!defined(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
  112:     $GET_ARRAY_ARGS_result.="list zero is undefined<br />";
  113:     $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
  114:   }
  115:   return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
  116: }
  117: 
  118: sub caparesponse_check_list {
  119:   my ($response,$expr)=@_;
  120:   my $result;
  121:   my ($result,@list) = &get_array_args($expr,'answer');
  122:   my $aresult='';
  123:   my $current_answer;
  124:   my $answers=join(':',@list);
  125:   $result.="Got response :$answers:<br />";
  126:   my @responselist;
  127:   my $type =eval $expr.';return $answer;';
  128:   if ($type ne '' && $#list > 0) {
  129:     (@responselist)=split /,/,$response;
  130:   } else {
  131:     (@responselist)=($response);
  132:   }
  133:   my $unit='';
  134:   $result.="Initial final response :$responselist['-1']:<br />";
  135:   if ($type eq '') {
  136:     #for numerical problems split off the unit
  137:     if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
  138:       $responselist['-1']=$1;
  139:       $unit=$2;
  140:     }
  141:   }
  142:   $result.="Final final response :$responselist['-1']:<br />";
  143:   $result.=":$#list: answers<br />";
  144:   $unit=~s/\s//;
  145:   my $i=0;
  146:   my $awards='';
  147:   for ($i=0; $i<@list;$i++) {
  148:     $result.="trying answer :$list[$i]:<br />";
  149:     if ($unit eq '') {
  150:       $aresult=&caparesponse_check($responselist[$i],
  151: 			     $expr.';my $answer=\''.$list[$i].'\';');
  152:     } else {
  153:       $aresult=&caparesponse_check($responselist[$i]." $unit",
  154: 				   $expr.';my $answer=\''.$list[$i].'\';');
  155:     }
  156:     my ($temp)=split /:/, $aresult;
  157:     $awards.="$temp,";
  158:     $result.=$aresult;
  159:   }
  160:   chop $awards;
  161:   return "$awards:<br />$result";
  162: }
  163: 
  164: sub tex {
  165:   if ( $external::target eq "tex" ) {
  166:     return @_[0];
  167:   } else {
  168:     return @_[1];
  169:   }
  170: }
  171: 
  172: sub var_in_tex {
  173:   if ( $external::target eq "tex" ) {
  174:     return @_[0];
  175:   } else {
  176:     return "";
  177:   }
  178: }
  179: 
  180: sub web {
  181:   if ( $external::target eq "tex" ) {
  182:     return @_[1];
  183:   } else {
  184:     if ( $external::target eq "web") {
  185:       return @_[2];
  186:     } else {
  187:       return @_[0];
  188:     }
  189:   }
  190: }
  191: 
  192: sub html {
  193:   if ( $external::target eq "web" ) {
  194:     return shift;
  195:   }
  196: }
  197: 
  198: sub problem {
  199:   return '1';
  200: }
  201: 
  202: sub hinton {
  203:   return 0;
  204: }
  205: 
  206: sub random {
  207:   my ($start,$end,$step)=@_;
  208:   if ( ! $hidden::RANDOMINIT ) {
  209:     srand($external::randomseed);
  210:     $hidden::RANDOMINIT=1;
  211:   }
  212:   my $num=1+int(($end-$start)/$step);
  213:   my $result=$start + int(rand() * $num)*$step;
  214:   return $result;
  215: }
  216: 
  217: sub random_normal {
  218:   my ($item_cnt,$seed,$av,$std_dev) = @_;
  219:   my @retArray;
  220:   &random_set_seed_from_phrase($seed);
  221:   @retArray=&math_random_normal($item_cnt,$av,$std_dev);
  222:   return @retArray;
  223: }
  224: 
  225: sub random_beta {
  226:   my ($item_cnt,$seed,$aa,$bb) = @_;
  227:   my @retArray;
  228:   &random_set_seed_from_phrase($seed);
  229:   @retArray=&math_random_beta($item_cnt,$aa,$bb);
  230:   return @retArray;
  231: }
  232: 
  233: sub random_gamma {
  234:   my ($item_cnt,$seed,$a,$r) = @_;
  235:   my @retArray;
  236:   &random_set_seed_from_phrase($seed);
  237:   @retArray=&math_random_gamma($item_cnt,$a,$r);
  238:   return @retArray;
  239: }
  240: 
  241: sub random_exponential {
  242:   my ($item_cnt,$seed,$av) = @_;
  243:   my @retArray;
  244:   &random_set_seed_from_phrase($seed);
  245:   @retArray=&math_random_exponential($item_cnt,$av);
  246:   return @retArray;
  247: }
  248: 
  249: sub random_poisson {
  250:   my ($item_cnt,$seed,$mu) = @_;
  251:   my @retArray;
  252:   &random_set_seed_from_phrase($seed);
  253:   @retArray=&math_random_poisson($item_cnt,$mu);
  254:   return @retArray;
  255: }
  256: 
  257: sub random_chi {
  258:   my ($item_cnt,$seed,$df) = @_;
  259:   my @retArray;
  260:   &random_set_seed_from_phrase($seed);
  261:   @retArray=&math_random_chi_square($item_cnt,$df);
  262:   return @retArray;
  263: }
  264: 
  265: sub random_noncentral_chi {
  266:   my ($item_cnt,$seed,$df,$nonc) = @_;
  267:   my @retArray;
  268:   &random_set_seed_from_phrase($seed);
  269:   @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
  270:   return @retArray;
  271: }
  272: 
  273: sub random_f {
  274:   my ($item_cnt,$seed,$dfn,$dfd) = @_;
  275:   my @retArray;
  276:   &random_set_seed_from_phrase($seed);
  277:   @retArray=&math_random_f($item_cnt,$dfn,$dfd);
  278:   return @retArray;
  279: }
  280: 
  281: sub random_noncentral_f {
  282:   my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
  283:   my @retArray;
  284:   &random_set_seed_from_phrase($seed);
  285:   @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
  286:   return @retArray;
  287: }
  288: 
  289: sub random_multivariate_normal {
  290:   my ($item_cnt,$seed,$mean,$covar) = @_;
  291:   &random_set_seed_from_phrase($seed);
  292:   @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
  293:   return @retArray;
  294: }
  295: 
  296: sub random_multinomial {
  297:   my ($item_cnt,$seed,@p) = @_;
  298:   my @retArray;
  299:   &random_set_seed_from_phrase($seed);
  300:   @retArray=&math_random_multinomial($item_cnt,@p);
  301:   return @retArray;
  302: }
  303: 
  304: sub random_permutation {
  305:   my ($seed,@inArray) = @_;
  306:   my @retArray;
  307:   &random_set_seed_from_phrase($seed);
  308:   @retArray=&math_random_permutation(@inArray);
  309:   return @retArray;
  310: }
  311: 
  312: sub random_uniform {
  313:   my ($item_cnt,$seed,$low,$high) = @_;
  314:   my @retArray;
  315:   &random_set_seed_from_phrase($seed);
  316:   @retArray=&math_random_uniform($item_cnt,$low,$high);
  317:   return @retArray;
  318: }
  319: 
  320: sub random_uniform_integer {
  321:   my ($item_cnt,$seed,$low,$high) = @_;
  322:   my @retArray;
  323:   &random_set_seed_from_phrase($seed);
  324:   @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
  325:   return @retArray;
  326: }
  327: 
  328: sub random_binomial {
  329:   my ($item_cnt,$seed,$nt,$p) = @_;
  330:   my @retArray;
  331:   &random_set_seed_from_phrase($seed);
  332:   @retArray=&math_random_binomial($item_cnt,$nt,$p);
  333:   return @retArray;
  334: }
  335: 
  336: sub random_negative_binomial {
  337:   my ($item_cnt,$seed,$ne,$p) = @_;
  338:   my @retArray;
  339:   &random_set_seed_from_phrase($seed);
  340:   @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
  341:   return @retArray;
  342: }
  343: 
  344: sub abs { abs(shift) }
  345: sub sin { sin(shift) }
  346: sub cos { cos(shift) }
  347: sub exp { exp(shift) }
  348: sub int { int(shift) }
  349: sub log { log(shift) }
  350: sub atan2 { atan2($_[0],$_[1]) }
  351: sub sqrt { sqrt(shift) }
  352: 
  353: sub tan  { sin($_[0]) / cos($_[0]) }
  354: #sub atan { atan2($_[0], 1); }
  355: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  356: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  357: 
  358: sub log10 { log($_[0])/log(10); }
  359: 
  360: sub factorial {
  361:     my $input = int(shift);
  362:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  363:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  364:     return 1 if $input == 0;
  365:     my $result = 1; 
  366:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  367:     return $result;
  368: }
  369: 
  370: sub sgn {
  371:     return -1 if $_[0] < 0;
  372:     return 0 if $_[0] == 0;
  373:     return 1 if $_[0] > 0;
  374: }
  375: 
  376: sub min {
  377:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  378:     return shift @sorted;
  379: }
  380: 
  381: sub max {
  382:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  383:     return pop @sorted;
  384: }
  385: 
  386: sub roundto {
  387:     my ($input,$n) = @_;
  388:     return sprintf('%.'.$n.'f',$input);
  389: }
  390: 
  391: sub to_string {
  392:     my ($input,$n) = @_;
  393:     return sprintf($input) if $n eq "";
  394:     $n = '.'.$n if $n !~ /^\./;
  395:     return sprintf('%'.$n,$input) if $n ne "";
  396: }
  397: 
  398: sub sub_string {
  399:     my ($str,$start,$len) = @_;
  400:     return substr($str,$start-1,$len);
  401: }
  402: 
  403: sub pow   {return $_[0] ** $_[1]; }
  404: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); }
  405: sub floor  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); }
  406: #sub floor {return int($_[0]); }
  407: 
  408: sub format {
  409:   my ($value,$fmt)=@_;
  410:   return sprintf('%.'.$fmt,$value);
  411: }
  412: 
  413: sub map {
  414:     my ($phrase,$dest,$source)=@_;
  415:     my @seed = &random_seed_from_phrase($phrase);
  416:     &random_set_seed(@seed);
  417:     my $destct = scalar(@$dest);
  418:     if (!$source) {
  419: 	my @output;
  420: 	my @idx = &math_random_permuted_index($destct);
  421: 	my $ctr = 0;
  422: 	while ($ctr < $destct) {
  423: 	    $output[$ctr] = $$dest[$idx[$ctr]];
  424: 	    $ctr++;
  425: 	}
  426: 	return @output;
  427:     } else {
  428: 	my $num = scalar(@$source);
  429: 	my @idx = &math_random_permuted_index($num);
  430: 	my $ctr = 0;
  431: 	my $tot = $num;
  432: 	$tot = $destct if $destct < $num;
  433: 	if (ref($$dest[0])) {
  434: 	    while ($ctr < $tot) {
  435: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
  436: 	        $ctr++;
  437:             }
  438:         } else {
  439: 	    while ($ctr < $tot) {
  440: 		$$dest[$ctr] = $$source[$idx[$ctr]];
  441: 		$ctr++;
  442: 	    }
  443: 	}
  444:     }
  445: }
  446: 
  447: sub rmap {
  448:     my ($phrase,$dest,$source)=@_;
  449:     my @seed = &random_seed_from_phrase($phrase);
  450:     &random_set_seed(@seed);
  451:     my $destct = scalar(@$dest);
  452:     if (!$source) {
  453: 	my @idx = &math_random_permuted_index($destct);
  454: 	my $ctr = 0;
  455: 	my @r_idx;
  456: 	while ($ctr < $destct) {
  457: 	    $r_idx[$idx[$ctr]] = $ctr;
  458: 	    $ctr++;
  459: 	}
  460: 	my @output;
  461: 	$ctr = 0;
  462: 	while ($ctr < $destct) {
  463: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
  464: 	    $ctr++;
  465: 	}
  466: 	return @output;
  467:     } else {
  468: 	my $num = scalar(@$source);
  469: 	my @idx = &math_random_permuted_index($num);
  470: 	my $ctr = 0;
  471: 	my $tot = $num;
  472: 	$tot = $destct if $destct < $num;
  473: 	my @r_idx;
  474: 	while ($ctr < $tot) {
  475: 	    $r_idx[$idx[$ctr]] = $ctr;
  476: 	    $ctr++;
  477: 	}
  478: 	$ctr = 0;
  479: 	if (ref($$dest[0])) {
  480: 	    while ($ctr < $tot) {
  481: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
  482: 	        $ctr++;
  483:             }
  484:         } else {
  485: 	    while ($ctr < $tot) {
  486: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
  487: 		$ctr++;
  488: 	    }
  489: 	}
  490:     }
  491: }
  492: 
  493: sub capa_id { return }
  494: 
  495: sub problem { return }
  496: 
  497: sub name{
  498:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
  499:   $fullname = "" if $fullname eq ",  ";
  500:   $fullname =~ s/\%2d/-/g;
  501:   return $fullname;
  502: }
  503: 
  504: sub student_number { 
  505:   my $id = &EXT('environment.id');
  506:   $id = '' if $id eq "";
  507:   return $id;
  508: }
  509: 
  510: sub class {
  511:   my $course = &EXT('course.description');
  512:   $course = '' if $course eq "";
  513:   return $course;
  514: }
  515: 
  516: sub sec { 
  517:   my $sec = &EXT('request.course.sec');
  518:   $sec = '' if $sec eq "";
  519:   return $sec;
  520: }
  521: 
  522: sub open_date { 
  523:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
  524:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  525:   my @hm = split(/:/,$dc[3]);
  526:   my $ampm = " am";
  527:   if ($hm[0] > 12) {
  528:     $hm[0]-=12;
  529:     $ampm = " pm";
  530:   }
  531:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  532: }
  533: 
  534: sub due_date { 
  535:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
  536:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  537:   my @hm = split(/:/,$dc[3]);
  538:   my $ampm = " am";
  539:   if ($hm[0] > 12) {
  540:     $hm[0]-=12;
  541:     $ampm = " pm";
  542:   }
  543:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  544: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  545: }
  546: 
  547: sub answer_date { 
  548:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
  549:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  550:   my @hm = split(/:/,$dc[3]);
  551:   my $ampm = " am";
  552:   if ($hm[0] > 12) {
  553:     $hm[0]-=12;
  554:     $ampm = " pm";
  555:   }
  556:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  557: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  558: }
  559: 
  560: sub array_moments {
  561:   my @input=@_;
  562:   my (@output,$N);
  563:   $N=scalar (@input);
  564:   $output[0]=$N;
  565:   if ($N <= 1) {
  566:     $output[1]=$input[0];
  567:     $output[1]="Input array not defined" if ($N == 0);
  568:     $output[2]="variance undefined for N<=1";
  569:     $output[3]="skewness undefined for N<=1";
  570:     $output[4]="kurtosis undefined for N<=1";
  571:     return @output;
  572:   }
  573:   my $sum=0;
  574:   foreach my $line (@input) {
  575:     $sum+=$line;
  576:   }
  577:   $output[1] = $sum/$N;
  578:   my ($x,$sdev,$var,$skew,$kurt) = 0;
  579:   foreach my $line (@input) {
  580:     $x=$line-$output[1];
  581:     $var+=$x**2;
  582:     $skew+=$x**3;
  583:     $kurt+=$x**4;
  584:   }
  585:   $output[2]=$var/($N-1);
  586:   $sdev=sqrt($output[2]);
  587:   if ($sdev == 0) {
  588:      $output[3]="inf-variance=0";
  589:      $output[4]="inf-variance=0";
  590:      return @output;
  591:   }
  592:   $output[3]=$skew/($sdev**3*$N);
  593:   $output[4]=$kurt/($sdev**4*$N)-3;
  594:   return @output;
  595: }
  596: 
  597: sub choose {
  598:   my $num = $_[0];
  599:   return $_[$num];
  600: }
  601: 
  602: 

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