## Forum archive 2000-2006

### Tom Robbins - Multiple Correct Answers ### Tom Robbins - Multiple Correct Answers

by Arnold Pizer -
Number of replies: 0 Multiple Correct Answers topic started 7/10/2001; 7:05:37 PMlast post 8/22/2001; 4:29:26 PM Tom Robbins - Multiple Correct Answers 7/10/2001; 7:05:37 PM (reads: 2169, responses: 5) Hi WeBWorK Help, I have a question on how to score a problem with multiple correct answers. For example, if I ask for a unit vector that is orthogonal to the vectors [0,1,0] and [1,0,0], then the student should be able to enter [0,0,1] or [0,0,-1]. Thank you for your help, Tom <| Post or View Comments |> Zbigniew Fiedorowicz - Re: Multiple Correct Answers 7/11/2001; 8:29:32 AM (reads: 2543, responses: 0) You can do this using the NAMED_ANS construction. I attach a simple example below. Zig DOCUMENT(); #Problem with two correct answers loadMacros( ); $showPartialCorrectAnswers = 1; TEXT(&beginproblem); BEGIN_TEXT Find a unit vector perpendicular to (\frac35vec{i}+frac45vec{j}): [\mbox{{NAMED_ANS_RULE(first_answer,5)}}vec{i}+mbox{{ans_rule(5)}}vec{j}] END_TEXT #First peek at the i-component ($ans_eval1) = std_num_cmp(4/5); $firstAnswer =$inputs_ref->{first_answer}; $firstAnswer = '' unless defined($firstAnswer); $rh_ans_hash = &$ans_eval1($firstAnswer); #If i-component is 4/5, correct answer is 4/5 i - 3/5 j if (1 ==$rh_ans_hash->{score}) { &NAMED_ANS(first_answer,std_num_cmp(4/5)); &ANS(std_num_cmp(-3/5)); } else { #else the correct answer is -4/5 i + 3/5 j &NAMED_ANS(first_answer,std_num_cmp(-4/5)); &ANS(std_num_cmp(3/5)); } ENDDOCUMENT(); <| Post or View Comments |> Thomas R. Shemanske - Re: Multiple Correct Answers 8/14/2001; 5:31:20 PM (reads: 2470, responses: 0) Zig's code works great for both numerical and string answers. I was blindly trying to modify it to work with functions. I have included the pg source (below) along with the error message. Note that the backslashes have been removed by the posting program. After fussing about, it is clear that the offending line is line 56 56 ($ans_eval1) = function_cmp($function1); which means the answer evaluator returned by function_cmp is different than that of num_cmp and str_cmp. I have stared at PGanswermacros.pl long enough to know I haven't a clue how to fix this. If there are suggestiongs out there, I would appreciate hearing. Thanks, Tom Problem4 ERROR caught by PGtranslator while processing problem file:setf01Ch18S1/problem_4.pg * Not a CODE reference at (eval 49) line 57. * ------Input Read 1 ## -*- perl -*- ## 2 ## Line above puts emacs in perl mode 3 ## 4 ## Description 5 ## Second Order Constant Coefficient ODE (distinct real roots) 6 ## EndDescription 7 8 DOCUMENT(); 9 loadMacros(PG.pl, 15 "Dartmouthmacros.pl"); 16 17 18 ## Do NOT show partial correct answers 19 $showPartialCorrectAnswers = 0; 20 21 22$root1 = non_zero_random(-10,10); 23 $root2 =$root1 + random(1,5); 24 ## If $root2 is zero, then$root1 < 0 25 if ($root2 == 0) {$root2 = random(1,10);} 26 $function1 = "exp($root1*x)"; 27 $function2 = "exp($root2*x)"; 28 29 ## Ok, we are ready to begin the problem... 30 ## 31 TEXT(beginproblem()); 32 33 34 BEGIN_TEXT 35 $BR 36 Find the general solution to the homogeneous differential equation 37 [ 38 frac{d^2y}{dx^2} - {$root1 + $root2}frac{dy}{dx} + {$root1 * $root2}y = 0 39 ] 40$BR 41 The solution has the form 42 [y = C_1 f_1(x) + C_2 f_2(x)] 43 with 44 (f_1(x) = ) {NAMED_ANS_RULE(first_fcn,25)} 45 and (f_2(x) =) {NAMED_ANS_RULE(second_fcn,25)} 46 $BR 47 48 Left to your own devices, you will probably write down the correct 49 answers, but in case you want to quibble, enter your answers so that 50 (f_1, f_2) are normalized with their value at (x = 0) equal to 51 1. 52 53$PAR 54 END_TEXT 55 56 ($ans_eval1) = function_cmp($function1); 57 $firstAnswer =$inputs_ref->{first_fcn}; 58 $firstAnswer = '' unless defined($firstAnswer); 59 60 $rh_ans_hash = &$ans_eval1($firstAnswer); 61 62 if ($rh_ans_hash->{score} == 1 ) 63 { 64 NAMED_ANS( first_fcn, fun_cmp($function1) ); 65 NAMED_ANS( second_fcn, fun_cmp($function2) ); 66 } 67 else 68 { 69 NAMED_ANS( second_fcn, fun_cmp($function2) ); 70 NAMED_ANS( first_fcn, fun_cmp($function1) ); 71 } 72 73 ENDDOCUMENT(); <| Post or View Comments |> Zbigniew Fiedorowicz - Re: Multiple Correct Answers 8/15/2001; 8:09:39 AM (reads: 2424, responses: 0) I wasn't able to get the NAMED_ANSWER construction to work with function_cmp() either. However I found that for the most part I could do what I wanted using std_num_cmp(). The basic idea is to use Perl's substitution operator s/.. to replace the variable(s) in the NAMED_ANSWER with carefully chosen number(s), then feed the result to the reference to std_num_cmp(). In most cases this suffices to distinguish between variant answers and react accordingly. For an example of this look at (I've put a number of NAMED_ANSWER examples in that directory.) Zig Fiedorowicz <| Post or View Comments |> Michael Gage - Re: Multiple Correct Answers 8/20/2001; 5:31:52 PM (reads: 2416, responses: 0) As an initial response to Tom's post -- there are two kinds of answer evaluators. (If you care the old version of answer evaluators is called a closure and behaves like an anonymous subroutine --see chapter 4 (p253 in 2nd edition of Learning Perl). The new version is an object labeled 'AnswerEvaluator' which is defined in the file AnswerHash.pm (near the end). The new version allows new features, such as the ability to add "debug=>1" as an argument to trouble shoot answer evaluators, and makes it easier to modify old answer evaluators to make new ones. ) Unfortunately the two types of answer evaluators have different calling syntax. Here is a snippet of code which will handle both types, although as time goes on I expect all of the old style answer evaluators to be replaced by the new style ones: if ( ref($ans_evaluator) eq 'AnswerEvaluator' ) { # new style$rh_ans_hash = $ans_evaluator->evaluate($student_answer);} elsif (ref($ans_eval) eq 'CODE' ) { #old style$rh_ans_hash = &$ans_evaluator($student_answer);} else { warn "There is a problem using the answer evaluator";} <| Post or View Comments |> Zbigniew Fiedorowicz - Re: Multiple Correct Answers 8/22/2001; 4:29:26 PM (reads: 2381, responses: 0) I've now incorporated Mike's code in my examples at the URL I've wrapped it in a subroutine, so it can be invoked as &calleval($ans_evaluator,$student_answer); If you are using my previous code, please update to this new version so your problems don't break when a new version of WeBWorK with the new style evaluators is rolled out. Zig Fiedorowicz sub calleval { my ($ans_evaluator,$student_answer) = @_; my $rh_ans_hash = ""; if ( ref($ans_evaluator) eq 'AnswerEvaluator' ) { # new style $rh_ans_hash =$ans_evaluator->evaluate($student_answer); } elsif (ref($ans_evaluator) eq 'CODE' ) { #old style $rh_ans_hash = &$ans_evaluator($student_answer); } else { warn "There is a problem using the answer evaluator"; } return$rh_ans_hash; } <| Post or View Comments |>