Annotation of loncom/homework/default_homework.lcpm, revision 1.20
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.20 ! harris41 6: #use Math::Trig;
! 7: #
! 8: # Guy Albertelli
! 9: #
! 10: # 05/25/2001 H. K. Ng
! 11: #
1.3 albertel 12: $RANDOMINIT=0;
1.16 albertel 13: $pi=atan2(1,1)*4;
1.3 albertel 14:
1.7 albertel 15: sub caparesponse_check {
1.10 albertel 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
1.19 albertel 20: my $sig=''; #done lowerbnd,upperbnd
21: my $sig_lbound=''; #done
22: my $sig_ubound=''; #done
1.7 albertel 23: my $ans_fmt='';
1.19 albertel 24: my $units=''; #done
1.7 albertel 25: my $calc='';
26: my ($response,$expr)=@_;
27:
1.12 albertel 28: ($answer,$type,$tol,$sig,$ans_fmt,
29: $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
1.8 albertel 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;
1.9 albertel 39: } elsif ($type eq 'subj') { $type = 7;
40: } else { return "ERROR: Unknown type of answer: $type" }
1.8 albertel 41: }
42:
1.10 albertel 43: if ($tol eq '') {
44: $tol=0.0;
45: $tol_type=1; #TOL_ABSOLUTE
46: } else {
47: if ($tol =~ /%$/) {
1.12 albertel 48: chop $tol;
1.10 albertel 49: $tol_type=2; #TOL_PERCENTAGE
50: } else {
51: $tol_type=1; #TOL_ABSOLUTE
52: }
53: }
1.12 albertel 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: }
1.7 albertel 61: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10 albertel 62: $tol_type,$tol,
1.7 albertel 63: $sig_lbound,$sig_ubound,
1.12 albertel 64: $ans_fmt,$units,$calc);
1.9 albertel 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'; }
1.13 albertel 78: else {$result = "ERROR: Unknown Result:$result:$@:";}
1.9 albertel 79:
1.17 albertel 80: 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 81: }
82:
83: sub caparesponse_check_list {
84: my ($response,$expr)=@_;
85: my $result='';
1.15 albertel 86: my $aresult='';
1.14 albertel 87: my $current_answer;
88: my $answer = eval $expr.';return $answer';
89: my (@list) = eval $answer;
90: my (@responselist)=split /,/,$response;
1.15 albertel 91: my $unit='';
1.17 albertel 92: $result.="Final response :$responselist['-1']:<br />";
1.15 albertel 93: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
94: $responselist['-1']=$1;
95: $unit=$2;
96: }
1.17 albertel 97: $result.="Final response :$responselist['-1']:<br />";
1.14 albertel 98: $unit=~s/\s//;
99: my $i=0;
100: my $awards='';
101: for ($i=0; $i<@list;$i++) {
1.19 albertel 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: }
1.15 albertel 109: my ($temp)=split /:/, $aresult;
1.14 albertel 110: $awards.="$temp,";
1.15 albertel 111: $result.=$aresult;
1.14 albertel 112: }
113: chop $awards;
1.17 albertel 114: return "$awards:<br />$result";
1.7 albertel 115: }
116:
1.4 albertel 117: sub tex {
118: if ( $external::target eq "tex" ) {
119: return @_[0];
120: } else {
121: return @_[1];
122: }
123: }
124:
125: sub web {
126: if ( $external::target eq "tex" ) {
127: return @_[1];
128: } else {
129: if ( $external::target eq "web") {
130: return @_[0];
131: } else {
132: return @_[2];
133: }
134: }
135: }
136:
137: sub problem {
1.11 albertel 138: return '1';
1.4 albertel 139: }
140:
1.1 harris41 141: sub hinton {
142: return 0;
143: }
144:
145: sub random {
146: my ($start,$end,$step)=@_;
1.3 albertel 147: if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
1.1 harris41 148: my $num=1+int(($end-$start)/$step);
149: my $result=$start + int(rand() * $num)*$step;
150: return $result;
151: }
152:
153: sub tan { sin($_[0]) / cos($_[0]) }
1.18 albertel 154: sub atan { atan2($_[0], 1); }
1.1 harris41 155: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
156: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.18 albertel 157: sub log10 { log($_[0])/log(10); }
1.20 ! harris41 158: sub factorial {
! 159: my $input = int(shift);
! 160: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
! 161: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
! 162: return 1 if $input == 0;
! 163: my $result = 1;
! 164: for (my $i=2; $i<=$input; $i++) { $result *= $i }
! 165: return $result;
! 166: }
! 167:
! 168: sub sgn {
! 169: return -1 if $_[0] < 0;
! 170: return 0 if $_[0] == 0;
! 171: return 1 if $_[0] > 0;
! 172: }
! 173:
! 174: sub min {
! 175: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
! 176: return shift @sorted;
! 177: }
! 178:
! 179: sub max {
! 180: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
! 181: return pop @sorted;
! 182: }
1.1 harris41 183:
184: sub html {
1.4 albertel 185: if ( $external::target eq "web" ) {
186: return @_[0];
1.1 harris41 187: }
188: }
189:
1.20 ! harris41 190: sub roundto {
! 191: my ($input,$n) = @_;
! 192: return sprintf('%.'.$n.'f',$input);
! 193: }
! 194:
! 195: sub to_string {
! 196: my ($input,$n) = @_;
! 197: return sprintf('%'.$n,$input) if $n ne "";
! 198: return sprintf($input) if $n eq "";
! 199: }
! 200:
! 201: sub sub_string {
! 202: my ($str,$start,$len) = @_;
! 203: return substr($str,$start-1,$len);
! 204: }
1.1 harris41 205:
206: sub pow {return $_[0] ** $_[1]; }
207: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
208: sub floor {return int($_[0]); }
209:
1.2 albertel 210: sub format {
211: my ($value,$fmt)=@_;
1.11 albertel 212: return sprintf('%.'.$fmt,$value);
1.2 albertel 213: }
1.5 albertel 214:
215: sub map {
216: my $num = $#_/2;
217: my $i;
1.6 albertel 218: my @used;
219: my @permutation;
220: for ($i=0; $i < $num;) {
221: $rand = &random(1,$num,1);
222: if ( $used[$rand] == '0' ) {
223: $i++;
224: $used[$rand]='1';
225: $permutation[$i]=$rand;
226: }
227: }
1.5 albertel 228: for ( $i=1 ; $i<$num+1 ; $i++ ) {
1.6 albertel 229: ${$_[$permutation[$i]]}=$_[$i+$num]
1.5 albertel 230: }
231: }
232:
233: sub choose {
234: my $num = $_[0];
235: return $_[$num];
236: }
1.6 albertel 237: #$external::randomseed=time;
1.5 albertel 238: #$a=2;
239: #$b=2;
1.6 albertel 240: #$c=2;
241: #&map(1,\$a,\$b,\$c,3,4,5);
1.5 albertel 242: #print $a."\n".$b."\n";
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>