Annotation of loncom/homework/default_homework.lcpm, revision 1.17
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.12 albertel 14: my $sig='';
1.7 albertel 15: my $sig_lbound='';
16: my $sig_ubound='';
17: my $ans_fmt='';
1.9 albertel 18: my $units='';
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.15 albertel 96: $aresult=&caparesponse_check($responselist[$i]." $unit",
1.14 albertel 97: $expr.';my $answer='.$list[$i].';');
1.15 albertel 98: my ($temp)=split /:/, $aresult;
1.14 albertel 99: $awards.="$temp,";
1.15 albertel 100: $result.=$aresult;
1.14 albertel 101: }
102: chop $awards;
1.17 ! albertel 103: return "$awards:<br />$result";
1.7 albertel 104: }
105:
1.4 albertel 106: sub tex {
107: if ( $external::target eq "tex" ) {
108: return @_[0];
109: } else {
110: return @_[1];
111: }
112: }
113:
114: sub web {
115: if ( $external::target eq "tex" ) {
116: return @_[1];
117: } else {
118: if ( $external::target eq "web") {
119: return @_[0];
120: } else {
121: return @_[2];
122: }
123: }
124: }
125:
126: sub problem {
1.11 albertel 127: return '1';
1.4 albertel 128: }
129:
1.1 harris41 130: sub hinton {
131: return 0;
132: }
133:
134: sub random {
135: my ($start,$end,$step)=@_;
1.3 albertel 136: if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
1.1 harris41 137: my $num=1+int(($end-$start)/$step);
138: my $result=$start + int(rand() * $num)*$step;
139: return $result;
140: }
141:
142: sub tan { sin($_[0]) / cos($_[0]) }
143: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
144: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
145:
146: sub html {
1.4 albertel 147: if ( $external::target eq "web" ) {
148: return @_[0];
1.1 harris41 149: }
150: }
151:
152:
153: sub pow {return $_[0] ** $_[1]; }
154: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
155: sub floor {return int($_[0]); }
156:
1.2 albertel 157: sub format {
158: my ($value,$fmt)=@_;
1.11 albertel 159: return sprintf('%.'.$fmt,$value);
1.2 albertel 160: }
1.5 albertel 161:
162: sub map {
163: my $num = $#_/2;
164: my $i;
1.6 albertel 165: my @used;
166: my @permutation;
167: for ($i=0; $i < $num;) {
168: $rand = &random(1,$num,1);
169: if ( $used[$rand] == '0' ) {
170: $i++;
171: $used[$rand]='1';
172: $permutation[$i]=$rand;
173: }
174: }
1.5 albertel 175: for ( $i=1 ; $i<$num+1 ; $i++ ) {
1.6 albertel 176: ${$_[$permutation[$i]]}=$_[$i+$num]
1.5 albertel 177: }
178: }
179:
180: sub choose {
181: my $num = $_[0];
182: return $_[$num];
183: }
1.6 albertel 184: #$external::randomseed=time;
1.5 albertel 185: #$a=2;
186: #$b=2;
1.6 albertel 187: #$c=2;
188: #&map(1,\$a,\$b,\$c,3,4,5);
1.5 albertel 189: #print $a."\n".$b."\n";
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>