Annotation of loncom/homework/default_homework.lcpm, revision 1.19
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.16 albertel 5: #init some globals
1.3 albertel 6: $RANDOMINIT=0;
1.16 albertel 7: $pi=atan2(1,1)*4;
1.3 albertel 8:
1.7 albertel 9: sub caparesponse_check {
1.10 albertel 10: my $answer=''; #done
11: my $type=''; #done
12: my $tol_type=''; # gets it's value from whether tol has a % or not done
13: my $tol=''; #done
1.19 ! albertel 14: my $sig=''; #done lowerbnd,upperbnd
! 15: my $sig_lbound=''; #done
! 16: my $sig_ubound=''; #done
1.7 albertel 17: my $ans_fmt='';
1.19 ! albertel 18: my $units=''; #done
1.7 albertel 19: my $calc='';
20: my ($response,$expr)=@_;
21:
1.12 albertel 22: ($answer,$type,$tol,$sig,$ans_fmt,
23: $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
1.8 albertel 24: #type's definitons come from capaParser.h
25: if ($type eq '' ) {
26: if ( $answer eq ($answer *1.0)) { $type = 2;
27: } else { $type = 3; }
28: } else {
29: if ($type eq 'cs') { $type = 4;
30: } elsif ($type eq 'ci') { $type = 3;
31: } elsif ($type eq 'mc') { $type = 5;
32: } elsif ($type eq 'fml') { $type = 8;
1.9 albertel 33: } elsif ($type eq 'subj') { $type = 7;
34: } else { return "ERROR: Unknown type of answer: $type" }
1.8 albertel 35: }
36:
1.10 albertel 37: if ($tol eq '') {
38: $tol=0.0;
39: $tol_type=1; #TOL_ABSOLUTE
40: } else {
41: if ($tol =~ /%$/) {
1.12 albertel 42: chop $tol;
1.10 albertel 43: $tol_type=2; #TOL_PERCENTAGE
44: } else {
45: $tol_type=1; #TOL_ABSOLUTE
46: }
47: }
1.12 albertel 48:
49: if ($sig eq '') {
50: $sig_lbound = 0; #SIG_LB_DEFAULT
51: $sig_ubound =15; #SIG_UB_DEFAULT
52: } else {
53: ($sig_lbound,$sig_ubound) = split /,/,$sig;
54: }
1.7 albertel 55: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10 albertel 56: $tol_type,$tol,
1.7 albertel 57: $sig_lbound,$sig_ubound,
1.12 albertel 58: $ans_fmt,$units,$calc);
1.9 albertel 59:
60: if ($result == '1') { $result='EXACT_ANS'; }
61: elsif ($result == '2') { $result='APPROX_ANS'; }
62: elsif ($result == '3') { $result='SIG_FAIL'; }
63: elsif ($result == '4') { $result='UNIT_FAIL'; }
64: elsif ($result == '5') { $result='NO_UNIT'; }
65: elsif ($result == '6') { $result='UNIT_OK'; }
66: elsif ($result == '7') { $result='INCORRECT'; }
67: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
68: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
69: elsif ($result =='10') { $result='SUB_RECORDED'; }
70: elsif ($result =='11') { $result='BAD_FORMULA'; }
71: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.13 albertel 72: else {$result = "ERROR: Unknown Result:$result:$@:";}
1.9 albertel 73:
1.17 albertel 74: return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br />$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br />";
1.14 albertel 75: }
76:
77: sub caparesponse_check_list {
78: my ($response,$expr)=@_;
79: my $result='';
1.15 albertel 80: my $aresult='';
1.14 albertel 81: my $current_answer;
82: my $answer = eval $expr.';return $answer';
83: my (@list) = eval $answer;
84: my (@responselist)=split /,/,$response;
1.15 albertel 85: my $unit='';
1.17 albertel 86: $result.="Final response :$responselist['-1']:<br />";
1.15 albertel 87: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
88: $responselist['-1']=$1;
89: $unit=$2;
90: }
1.17 albertel 91: $result.="Final response :$responselist['-1']:<br />";
1.14 albertel 92: $unit=~s/\s//;
93: my $i=0;
94: my $awards='';
95: for ($i=0; $i<@list;$i++) {
1.19 ! albertel 96: if ($unit eq '') {
! 97: $aresult=&caparesponse_check($responselist[$i],
! 98: $expr.';my $answer='.$list[$i].';');
! 99: } else {
! 100: $aresult=&caparesponse_check($responselist[$i]." $unit",
! 101: $expr.';my $answer='.$list[$i].';');
! 102: }
1.15 albertel 103: my ($temp)=split /:/, $aresult;
1.14 albertel 104: $awards.="$temp,";
1.15 albertel 105: $result.=$aresult;
1.14 albertel 106: }
107: chop $awards;
1.17 albertel 108: return "$awards:<br />$result";
1.7 albertel 109: }
110:
1.4 albertel 111: sub tex {
112: if ( $external::target eq "tex" ) {
113: return @_[0];
114: } else {
115: return @_[1];
116: }
117: }
118:
119: sub web {
120: if ( $external::target eq "tex" ) {
121: return @_[1];
122: } else {
123: if ( $external::target eq "web") {
124: return @_[0];
125: } else {
126: return @_[2];
127: }
128: }
129: }
130:
131: sub problem {
1.11 albertel 132: return '1';
1.4 albertel 133: }
134:
1.1 harris41 135: sub hinton {
136: return 0;
137: }
138:
139: sub random {
140: my ($start,$end,$step)=@_;
1.3 albertel 141: if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
1.1 harris41 142: my $num=1+int(($end-$start)/$step);
143: my $result=$start + int(rand() * $num)*$step;
144: return $result;
145: }
146:
147: sub tan { sin($_[0]) / cos($_[0]) }
1.18 albertel 148: sub atan { atan2($_[0], 1); }
1.1 harris41 149: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
150: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.18 albertel 151: sub log10 { log($_[0])/log(10); }
1.1 harris41 152:
153: sub html {
1.4 albertel 154: if ( $external::target eq "web" ) {
155: return @_[0];
1.1 harris41 156: }
157: }
158:
159:
160: sub pow {return $_[0] ** $_[1]; }
161: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
162: sub floor {return int($_[0]); }
163:
1.2 albertel 164: sub format {
165: my ($value,$fmt)=@_;
1.11 albertel 166: return sprintf('%.'.$fmt,$value);
1.2 albertel 167: }
1.5 albertel 168:
169: sub map {
170: my $num = $#_/2;
171: my $i;
1.6 albertel 172: my @used;
173: my @permutation;
174: for ($i=0; $i < $num;) {
175: $rand = &random(1,$num,1);
176: if ( $used[$rand] == '0' ) {
177: $i++;
178: $used[$rand]='1';
179: $permutation[$i]=$rand;
180: }
181: }
1.5 albertel 182: for ( $i=1 ; $i<$num+1 ; $i++ ) {
1.6 albertel 183: ${$_[$permutation[$i]]}=$_[$i+$num]
1.5 albertel 184: }
185: }
186:
187: sub choose {
188: my $num = $_[0];
189: return $_[$num];
190: }
1.6 albertel 191: #$external::randomseed=time;
1.5 albertel 192: #$a=2;
193: #$b=2;
1.6 albertel 194: #$c=2;
195: #&map(1,\$a,\$b,\$c,3,4,5);
1.5 albertel 196: #print $a."\n".$b."\n";
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>