## DBsubject('WeBWorK') ## DBchapter('Demos') ## DBsection('Problem') ## KEYWORDS() ## Author('Davor Cubranic') ## Institution('University of British Columbia') DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", ); ### Problem setup Context("Numeric"); $meaning_of_life = List(Real(0), Real(42)); ### Displayed text TEXT(beginproblem()); Context()->texStrings; BEGIN_TEXT What is the meaning of life (even if you're a nihilist)? $PAR \{ ans_rule(35) \} END_TEXT Context()->normalStrings; ### Answer and solution ANS($meaning_of_life->cmp(list_checker => sub { my ($correct, $student, $ansHash) = @_; my $score = 0; my @errors; # stores error messages if (scalar(@{$student} == 1)) { my $answer = ${$student}[0]; # iterate through correct values and compare them to # the student's answer foreach my $value (@{$correct}) { # this uses the currently active tolerance value # (e.g., as set for the context or the specific Real MathObject) if (($value <=> $answer) == 0) { # WebWork interprets "score" as the number of correct # values in the student's answer. In this case, getting # one value correct should be treated as getting all of # them correct: $score = scalar(@{$correct}); last; } } # if we get to here and the score is still 0, the # student's answer didn't match any of the correct values if ($score == 0 && ! $ansHash->{isPreview}) { push @errors, "Not correct"; } } elsif (! $ansHash->{isPreview}) { push @errors, "You have to enter a single number"; } return ($score, @errors); })); Context()->texStrings; BEGIN_SOLUTION According to Douglas Adams, the meaning of life is \( \{ $meaning_of_life->data->[1] \} \). Of course, if you're a nihilist, it's \( \{ $meaning_of_life->data->[0] \} \); END_SOLUTION Context()->normalStrings; ENDDOCUMENT();